]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/OOR.py
Added export decl
[wxWidgets.git] / wxPython / demo / OOR.py
CommitLineData
9416aa89 1
8fa876ca
RD
2import wx
3import wx.html as wxhtml
9416aa89
RD
4
5#----------------------------------------------------------------------
6
8fa876ca
RD
7BTN1 = wx.NewId()
8BTN2 = wx.NewId()
9416aa89 9
8fa876ca 10class TestPanel(wx.Panel):
9416aa89 11 def __init__(self, parent, log):
8fa876ca 12 wx.Panel.__init__(self, parent, -1)
9416aa89
RD
13 self.log = log
14
8fa876ca
RD
15 sizer = wx.BoxSizer(wx.VERTICAL)
16 html = wxhtml.HtmlWindow(self, -1)
9416aa89 17 html.SetPage(overview)
8fa876ca 18 sizer.Add(html, 1, wx.EXPAND|wx.ALL, 5)
9416aa89 19
8fa876ca
RD
20 btns = wx.BoxSizer(wx.HORIZONTAL)
21 btns.Add((50, -1), 1, wx.EXPAND)
22 btn1 = wx.Button(self, BTN1, "Find My Alter-ego") # don't save a ref to this one
9416aa89 23 btns.Add(btn1)
8fa876ca
RD
24 btns.Add((50, -1), 1, wx.EXPAND)
25 self.btn2 = wx.Button(self, BTN2, "Find Myself")
9416aa89 26 btns.Add(self.btn2)
8fa876ca 27 btns.Add((50, -1), 1, wx.EXPAND)
9416aa89 28
8fa876ca 29 sizer.Add(btns, 0, wx.EXPAND|wx.ALL, 15)
9416aa89
RD
30
31 self.SetSizer(sizer)
1e4a197e 32 self.SetAutoLayout(True)
9416aa89 33
2aab8f16
RD
34 self.sizer = sizer # save it for testing later
35
8fa876ca
RD
36 self.Bind(wx.EVT_BUTTON, self.OnFindButton1, id=BTN1)
37 self.Bind(wx.EVT_BUTTON, self.OnFindButton2, id=BTN2)
9416aa89
RD
38
39
40 def OnFindButton1(self, evt):
41 win = self.FindWindowById(BTN1)
8fa876ca 42
9416aa89
RD
43 if win is None:
44 self.log.write("***** OOPS! None returned...\n")
45 return
8fa876ca 46
9416aa89 47 className = win.__class__.__name__
8fa876ca
RD
48
49 if className in ["Button", "ButtonPtr"]:
9416aa89
RD
50 self.log.write("The types are the same! <grin>\n")
51 else:
52 self.log.write("Got %s, expected wxButton or wxButtonPtr\n" % className)
53
54
55
56 def OnFindButton2(self, evt):
57 win = self.FindWindowById(BTN2)
8fa876ca 58
9416aa89
RD
59 if win is None:
60 self.log.write("***** OOPS! None returned...\n")
61 return
8fa876ca 62
9416aa89
RD
63 if win is self.btn2:
64 self.log.write("The objects are the same! <grin>\n")
65 else:
66 self.log.write("The objects are NOT the same! <frown>\n")
67
0122b7e3 68 win = evt.GetEventObject()
8fa876ca 69
0122b7e3
RD
70 if win is None:
71 self.log.write("***** OOPS! None returned...\n")
72 return
8fa876ca 73
0122b7e3 74 if win is self.btn2:
2aab8f16
RD
75 self.log.write("The objects are the same! <grin>\n")
76 else:
77 self.log.write("The objects are NOT the same! <frown>\n")
78
79 sizer = self.GetSizer()
8fa876ca 80
2aab8f16
RD
81 if sizer is None:
82 self.log.write("***** OOPS! None returned...\n")
83 return
8fa876ca 84
2aab8f16 85 if sizer is self.sizer:
0122b7e3
RD
86 self.log.write("The objects are the same! <grin>\n")
87 else:
88 self.log.write("The objects are NOT the same! <frown>\n")
89
9416aa89
RD
90
91#----------------------------------------------------------------------
92
93def runTest(frame, nb, log):
94 win = TestPanel(nb, log)
95 return win
96
97#----------------------------------------------------------------------
98
99
100overview = """\
101<html><body>
102<h2>Original Object Return</h2>
103
104<p>Several methods in wxWindows return pointers to base class objects,
105when in fact the actual object pointed to is of a derived type. Since
106SWIG isn't able to tell the actual type it just creates a new Python
107shadow object of the base type to wrap around the base type pointer
108and returns it.
109
0122b7e3
RD
110<p>In wxPython prior to 2.3.0 this could cause annoying issues. For
111example if you called:
9416aa89
RD
112
113<pre>
114
115 myText = someWindow.FindWindowById(txtID)
116</pre>
117
0122b7e3 118expecting to get a wxTextCtrl you would actually get a wxWindow object
9416aa89
RD
119instead. If you then try to call SetValue on that object you'll get
120an exception since there is no such method. This is the reason for
121the wxPyTypeCast hack that has been in wxPython for so long.
122
0122b7e3
RD
123<p>Even with wxPyTypeCast there was the issue that the object returned
124was not the same one that was created in Python originally, but a new
9416aa89
RD
125object of the same type that wraps the same C++ pointer. If the
126programmer has set additional attributes of that original object they
127will not exist in the new object.
128
129<p>For a long time now I have wanted to do away with wxPyTypeCast and
130also find a way to return the original Python object from methods like
131FindWindowById. This project naturally divides into two phases:
132
133<p><ol>
134
135<li>Teach the wrapper methods how to return objects of the right type,
136and be able to then turn wxPyTypeCast in to a no-op.
137
138<li>Be able to return the original Python shadow object if it still exists.
139
140</ol>
141
142<p>The first button below shows the first of these phases (<i>working</i>)
8fa876ca 143and the second will show #2 (<i>working as of Python 2.3.2</i>)
9416aa89
RD
144
145</body></html>
146"""
1fded56b
RD
147
148
149
150
151if __name__ == '__main__':
152 import sys,os
153 import run
154 run.main(['', os.path.basename(sys.argv[0])])
155