]>
Commit | Line | Data |
---|---|---|
1 | # 11/13/2003 - Jeff Grimmett (grimmtooth@softhome.net) | |
2 | # | |
3 | # o Updated for wx namespace | |
4 | # | |
5 | ||
6 | import wx | |
7 | import wx.html as wxhtml | |
8 | ||
9 | #---------------------------------------------------------------------- | |
10 | ||
11 | BTN1 = wx.NewId() | |
12 | BTN2 = wx.NewId() | |
13 | ||
14 | class TestPanel(wx.Panel): | |
15 | def __init__(self, parent, log): | |
16 | wx.Panel.__init__(self, parent, -1) | |
17 | self.log = log | |
18 | ||
19 | sizer = wx.BoxSizer(wx.VERTICAL) | |
20 | html = wxhtml.HtmlWindow(self, -1) | |
21 | html.SetPage(overview) | |
22 | sizer.Add(html, 1, wx.EXPAND|wx.ALL, 5) | |
23 | ||
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 | |
27 | btns.Add(btn1) | |
28 | btns.Add((50, -1), 1, wx.EXPAND) | |
29 | self.btn2 = wx.Button(self, BTN2, "Find Myself") | |
30 | btns.Add(self.btn2) | |
31 | btns.Add((50, -1), 1, wx.EXPAND) | |
32 | ||
33 | sizer.Add(btns, 0, wx.EXPAND|wx.ALL, 15) | |
34 | ||
35 | self.SetSizer(sizer) | |
36 | self.SetAutoLayout(True) | |
37 | ||
38 | self.sizer = sizer # save it for testing later | |
39 | ||
40 | self.Bind(wx.EVT_BUTTON, self.OnFindButton1, id=BTN1) | |
41 | self.Bind(wx.EVT_BUTTON, self.OnFindButton2, id=BTN2) | |
42 | ||
43 | ||
44 | def OnFindButton1(self, evt): | |
45 | win = self.FindWindowById(BTN1) | |
46 | ||
47 | if win is None: | |
48 | self.log.write("***** OOPS! None returned...\n") | |
49 | return | |
50 | ||
51 | className = win.__class__.__name__ | |
52 | ||
53 | if className in ["Button", "ButtonPtr"]: | |
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) | |
62 | ||
63 | if win is None: | |
64 | self.log.write("***** OOPS! None returned...\n") | |
65 | return | |
66 | ||
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 | ||
72 | win = evt.GetEventObject() | |
73 | ||
74 | if win is None: | |
75 | self.log.write("***** OOPS! None returned...\n") | |
76 | return | |
77 | ||
78 | if win is self.btn2: | |
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() | |
84 | ||
85 | if sizer is None: | |
86 | self.log.write("***** OOPS! None returned...\n") | |
87 | return | |
88 | ||
89 | if sizer is self.sizer: | |
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 | ||
94 | ||
95 | #---------------------------------------------------------------------- | |
96 | ||
97 | def runTest(frame, nb, log): | |
98 | win = TestPanel(nb, log) | |
99 | return win | |
100 | ||
101 | #---------------------------------------------------------------------- | |
102 | ||
103 | ||
104 | overview = """\ | |
105 | <html><body> | |
106 | <h2>Original Object Return</h2> | |
107 | ||
108 | <p>Several methods in wxWindows return pointers to base class objects, | |
109 | when in fact the actual object pointed to is of a derived type. Since | |
110 | SWIG isn't able to tell the actual type it just creates a new Python | |
111 | shadow object of the base type to wrap around the base type pointer | |
112 | and returns it. | |
113 | ||
114 | <p>In wxPython prior to 2.3.0 this could cause annoying issues. For | |
115 | example if you called: | |
116 | ||
117 | <pre> | |
118 | ||
119 | myText = someWindow.FindWindowById(txtID) | |
120 | </pre> | |
121 | ||
122 | expecting to get a wxTextCtrl you would actually get a wxWindow object | |
123 | instead. If you then try to call SetValue on that object you'll get | |
124 | an exception since there is no such method. This is the reason for | |
125 | the wxPyTypeCast hack that has been in wxPython for so long. | |
126 | ||
127 | <p>Even with wxPyTypeCast there was the issue that the object returned | |
128 | was not the same one that was created in Python originally, but a new | |
129 | object of the same type that wraps the same C++ pointer. If the | |
130 | programmer has set additional attributes of that original object they | |
131 | will not exist in the new object. | |
132 | ||
133 | <p>For a long time now I have wanted to do away with wxPyTypeCast and | |
134 | also find a way to return the original Python object from methods like | |
135 | FindWindowById. 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, | |
140 | and 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>) | |
147 | and the second will show #2 (<i>working as of Python 2.3.2</i>) | |
148 | ||
149 | </body></html> | |
150 | """ | |
151 | ||
152 | ||
153 | ||
154 | ||
155 | if __name__ == '__main__': | |
156 | import sys,os | |
157 | import run | |
158 | run.main(['', os.path.basename(sys.argv[0])]) | |
159 |