]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/OOR.py
fixed restoration of global cursor
[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
35 EVT_BUTTON(self, BTN1, self.OnFindButton1)
36 EVT_BUTTON(self, BTN2, self.OnFindButton2)
37
38
39 def OnFindButton1(self, evt):
40 win = self.FindWindowById(BTN1)
41 if win is None:
42 self.log.write("***** OOPS! None returned...\n")
43 return
44 className = win.__class__.__name__
45 if className in ["wxButton", "wxButtonPtr"]:
46 self.log.write("The types are the same! <grin>\n")
47 else:
48 self.log.write("Got %s, expected wxButton or wxButtonPtr\n" % className)
49
50
51
52 def OnFindButton2(self, evt):
53 win = self.FindWindowById(BTN2)
54 if win is None:
55 self.log.write("***** OOPS! None returned...\n")
56 return
57 if win is self.btn2:
58 self.log.write("The objects are the same! <grin>\n")
59 else:
60 self.log.write("The objects are NOT the same! <frown>\n")
61
0122b7e3
RD
62 win = evt.GetEventObject()
63 if win is None:
64 self.log.write("***** OOPS! None returned...\n")
65 return
66 if win is self.btn2:
67 self.log.write("The objects are the same! <grin>\n")
68 else:
69 self.log.write("The objects are NOT the same! <frown>\n")
70
9416aa89
RD
71
72#----------------------------------------------------------------------
73
74def runTest(frame, nb, log):
75 win = TestPanel(nb, log)
76 return win
77
78#----------------------------------------------------------------------
79
80
81overview = """\
82<html><body>
83<h2>Original Object Return</h2>
84
85<p>Several methods in wxWindows return pointers to base class objects,
86when in fact the actual object pointed to is of a derived type. Since
87SWIG isn't able to tell the actual type it just creates a new Python
88shadow object of the base type to wrap around the base type pointer
89and returns it.
90
0122b7e3
RD
91<p>In wxPython prior to 2.3.0 this could cause annoying issues. For
92example if you called:
9416aa89
RD
93
94<pre>
95
96 myText = someWindow.FindWindowById(txtID)
97</pre>
98
0122b7e3 99expecting to get a wxTextCtrl you would actually get a wxWindow object
9416aa89
RD
100instead. If you then try to call SetValue on that object you'll get
101an exception since there is no such method. This is the reason for
102the wxPyTypeCast hack that has been in wxPython for so long.
103
0122b7e3
RD
104<p>Even with wxPyTypeCast there was the issue that the object returned
105was not the same one that was created in Python originally, but a new
9416aa89
RD
106object of the same type that wraps the same C++ pointer. If the
107programmer has set additional attributes of that original object they
108will not exist in the new object.
109
110<p>For a long time now I have wanted to do away with wxPyTypeCast and
111also find a way to return the original Python object from methods like
112FindWindowById. This project naturally divides into two phases:
113
114<p><ol>
115
116<li>Teach the wrapper methods how to return objects of the right type,
117and be able to then turn wxPyTypeCast in to a no-op.
118
119<li>Be able to return the original Python shadow object if it still exists.
120
121</ol>
122
123<p>The first button below shows the first of these phases (<i>working</i>)
0122b7e3 124and the second will show #2 (<i>working as of 2.3.2</i>)
9416aa89
RD
125
126</body></html>
127"""