]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/pyTree.py
OS/2 fixes
[wxWidgets.git] / wxPython / demo / pyTree.py
1 """
2 Hello, and welcome to this test of the wxTreeItemData
3 class.
4
5 The wxTreeItemData class can be used to associate a python
6 object with a wxTreeCtrl item. In this sample, its use is
7 demonstrated via a tree control that shows the contents of a
8 python namespace according to the standard dir()
9 command. Every item in the tree has its label taken from the
10 dir() output, and 'behind it' a reference to the python
11 object is stored in a wxTreeItemData object.
12
13 As you may have guessed by now, this sample automatically
14 displays '__doc__' strings if the selected python object
15 happens to have one. Please expand the pyTree object to
16 learn more about the implementation.
17
18 Version 1.0, April 4 1999.
19 Harm van der Heijden (H.v.d.Heijden@phys.tue.nl)
20
21 P.S. Check out the string module. It's imported in this
22 sample not because it's used, but because it's so
23 beautifully documented...
24 """
25
26 from wxPython import wx
27 import string # Don't use it, but it's fun expanding :-)
28
29 #----------------------------------------------------------------------
30
31 def _getindent(line):
32 """Returns the indentation level of the given line."""
33 indent = 0
34 for c in line:
35 if c == ' ': indent = indent + 1
36 elif c == '\t': indent = indent + 8
37 else: break
38 return indent
39
40 def _sourcefinder(func):
41 """Given a func_code object, this function tries to find and return
42 the python source code of the function."""
43 try:
44 f = open(func.co_filename,"r")
45 except:
46 return "(could not open file %s)" % (func.co_filename,)
47
48 for i in range(func.co_firstlineno):
49 line = f.readline()
50 ind = _getindent(line)
51 msg = ""
52 while line:
53 msg = msg + line
54 line = f.readline()
55 # the following should be <= ind, but then we get
56 # confused by multiline docstrings. Using == works most of
57 # the time... but not always!
58 if _getindent(line) == ind: break
59 return msg
60
61 #----------------------------------------------------------------------
62
63 class pyTree(wx.wxTreeCtrl):
64 """
65 This wxTreeCtrl derivative displays a tree view of a Python namespace.
66 Anything from which the dir() command returns a non-empty list is a branch
67 in this tree.
68 """
69
70 def __init__(self, parent, id, root):
71 """
72 Initialize function; because we insert branches into the tree
73 as needed, we use the ITEM_EXPANDING event handler. The
74 ITEM_COLLAPSED handler removes the stuff afterwards. The
75 SEL_CHANGED handler attempts to display interesting
76 information about the selected object.
77 """
78 wx.wxTreeCtrl.__init__(self, parent, id)
79 self.root = self.AddRoot(str(root), -1, -1, wx.wxTreeItemData(root))
80 if dir(root):
81 self.SetItemHasChildren(self.root, wx.TRUE)
82 wx.EVT_TREE_ITEM_EXPANDING(self, self.GetId(), self.OnItemExpanding)
83 wx.EVT_TREE_ITEM_COLLAPSED(self, self.GetId(), self.OnItemCollapsed)
84 wx.EVT_TREE_SEL_CHANGED(self, self.GetId(), self.OnSelChanged)
85 self.output = None
86
87
88 def SetOutput(self, output):
89 """
90 Set output function (accepts single string). Used to display string
91 representation of the selected object by OnSelChanged.
92 """
93 self.output = output
94
95
96 def OnItemExpanding(self,event):
97 """
98 The real workhorse of this class. First we retrieve the object
99 (parent) belonging to the branch that is to be expanded. This
100 is done by calling GetPyData(parent), which is a short-cut for
101 GetPyItemData(parent).Get().
102
103 Then we get the dir() list of that object. For each item in
104 this list, a tree item is created with associated
105 wxTreeItemData referencing the child object. We get this
106 object using child = getattr(parent, item).
107
108 Finally, we check wether the child returns a non-empty dir()
109 list. If so, it is labeled as 'having children', so that it
110 may be expanded. When it actually is expanded, this function
111 will again figure out what the offspring is.
112 """
113 item = event.GetItem()
114 obj = self.GetPyData( item )
115 lst = dir(obj)
116 for key in lst:
117 new_obj = getattr(obj,key)
118 new_item = self.AppendItem( item, key, -1, -1,
119 wx.wxTreeItemData(new_obj) )
120 if dir(new_obj):
121 self.SetItemHasChildren(new_item, wx.TRUE)
122
123 def OnItemCollapsed(self, event):
124 """
125 We need to remove all children here, otherwise we'll see all
126 that old rubbish again after the next expansion.
127 """
128 item = event.GetItem()
129 self.DeleteChildren(item)
130
131 def OnSelChanged(self, event):
132 """
133 If an output function is defined, we try to print some
134 informative, interesting and thought-provoking stuff to it.
135 If it has a __doc__ string, we print it. If it's a function or
136 unbound class method, we attempt to find the python source.
137 """
138 if not self.output:
139 return
140 obj = self.GetPyData( event.GetItem() )
141 msg = str(obj)
142 if hasattr(obj, '__doc__'):
143 msg = msg+"\n\nDocumentation string:\n\n%s" % ( getattr(obj, '__doc__'),)
144 # Is it a function?
145 func = None
146 if hasattr(obj, "func_code"): # normal function
147 func = getattr(obj, "func_code")
148 elif hasattr(obj, "im_func"): # unbound class method
149 func = getattr(getattr(obj, "im_func"), "func_code")
150 if func: # if we found one, let's try to print the source
151 msg = msg+"\n\nFunction source:\n\n" + _sourcefinder(func)
152
153 apply(self.output, (msg,))
154
155 #----------------------------------------------------------------------
156
157 overview = __doc__
158
159 def runTest(frame, nb, log):
160 """
161 This method is used by the wxPython Demo Framework for integrating
162 this demo with the rest.
163 """
164 thisModule = __import__(__name__, globals())
165 win = wx.wxFrame(frame, -1, "PyTreeItemData Test")
166 split = wx.wxSplitterWindow(win, -1)
167 tree = pyTree(split, -1, thisModule)
168 text = wx.wxTextCtrl(split, -1, "", wx.wxDefaultPosition,
169 wx.wxDefaultSize, wx.wxTE_MULTILINE)
170 split.SplitVertically(tree, text, 200)
171 tree.SetOutput(text.SetValue)
172 tree.SelectItem(tree.root)
173 win.SetSize(wx.wxSize(800,500))
174 frame.otherWin = win
175 win.Show(1)
176
177
178
179 #----------------------------------------------------------------------
180 if __name__ == '__main__':
181
182 class MyFrame(wx.wxFrame):
183 """Very standard Frame class. Nothing special here!"""
184
185 def __init__(self):
186 """Make a splitter window; left a tree, right a textctrl. Wow."""
187 import __main__
188 wx.wxFrame.__init__(self, wx.NULL, -1, "PyTreeItemData Test",
189 wx.wxDefaultPosition, wx.wxSize(800,500))
190 split = wx.wxSplitterWindow(self, -1)
191 tree = pyTree(split, -1, __main__)
192 text = wx.wxTextCtrl(split, -1, "", wx.wxDefaultPosition,
193 wx.wxDefaultSize, wx.wxTE_MULTILINE)
194 split.SplitVertically(tree, text, 200)
195 tree.SetOutput(text.SetValue)
196 tree.SelectItem(tree.root)
197
198 class MyApp(wx.wxApp):
199 """This class is even less interesting than MyFrame."""
200
201 def OnInit(self):
202 """OnInit. Boring, boring, boring!"""
203 frame = MyFrame()
204 frame.Show(wx.TRUE)
205 self.SetTopWindow(frame)
206 return wx.TRUE
207
208 app = MyApp(0)
209 app.MainLoop()
210
211