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