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