]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/OOR.py
   3 import  wx
.html 
as wxhtml
 
   5 #---------------------------------------------------------------------- 
  10 class TestPanel(wx
.Panel
): 
  11     def __init__(self
, parent
, log
): 
  12         wx
.Panel
.__init
__(self
, parent
, -1) 
  15         sizer 
= wx
.BoxSizer(wx
.VERTICAL
) 
  16         html 
= wxhtml
.HtmlWindow(self
, -1) 
  17         html
.SetPage(overview
) 
  18         sizer
.Add(html
, 1, wx
.EXPAND|wx
.ALL
, 5) 
  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  
  24         btns
.Add((50, -1), 1, wx
.EXPAND
) 
  25         self
.btn2 
= wx
.Button(self
, BTN2
, "Find Myself") 
  27         btns
.Add((50, -1), 1, wx
.EXPAND
) 
  29         sizer
.Add(btns
, 0, wx
.EXPAND|wx
.ALL
, 15) 
  32         self
.SetAutoLayout(True) 
  34         self
.sizer 
= sizer  
# save it for testing later 
  36         self
.Bind(wx
.EVT_BUTTON
, self
.OnFindButton1
, id=BTN1
) 
  37         self
.Bind(wx
.EVT_BUTTON
, self
.OnFindButton2
, id=BTN2
) 
  40     def OnFindButton1(self
, evt
): 
  41         win 
= self
.FindWindowById(BTN1
) 
  44             self
.log
.write("***** OOPS! None returned...\n") 
  47         className 
= win
.__class
__.__name
__ 
  49         if className 
in ["Button", "ButtonPtr"]: 
  50             self
.log
.write("The types are the same! <grin>\n") 
  52             self
.log
.write("Got %s, expected wxButton or wxButtonPtr\n" % className
) 
  56     def OnFindButton2(self
, evt
): 
  57         win 
= self
.FindWindowById(BTN2
) 
  60             self
.log
.write("***** OOPS! None returned...\n") 
  64             self
.log
.write("The objects are the same! <grin>\n") 
  66             self
.log
.write("The objects are NOT the same! <frown>\n") 
  68         win 
= evt
.GetEventObject() 
  71             self
.log
.write("***** OOPS! None returned...\n") 
  75             self
.log
.write("The objects are the same! <grin>\n") 
  77             self
.log
.write("The objects are NOT the same! <frown>\n") 
  79         sizer 
= self
.GetSizer() 
  82             self
.log
.write("***** OOPS! None returned...\n") 
  85         if sizer 
is self
.sizer
: 
  86             self
.log
.write("The objects are the same! <grin>\n") 
  88             self
.log
.write("The objects are NOT the same! <frown>\n") 
  91 #---------------------------------------------------------------------- 
  93 def runTest(frame
, nb
, log
): 
  94     win 
= TestPanel(nb
, log
) 
  97 #---------------------------------------------------------------------- 
 102 <h2>Original Object Return</h2> 
 104 <p>Several methods in wxWindows return pointers to base class objects, 
 105 when in fact the actual object pointed to is of a derived type.  Since 
 106 SWIG isn't able to tell the actual type it just creates a new Python 
 107 shadow object of the base type to wrap around the base type pointer 
 110 <p>In wxPython prior to 2.3.0 this could cause annoying issues.  For 
 111 example if you called: 
 115         myText = someWindow.FindWindowById(txtID) 
 118 expecting to get a wxTextCtrl you would actually get a wxWindow object 
 119 instead.  If you then try to call SetValue on that object you'll get 
 120 an exception since there is no such method.  This is the reason for 
 121 the wxPyTypeCast hack that has been in wxPython for so long. 
 123 <p>Even with wxPyTypeCast there was the issue that the object returned 
 124 was not the same one that was created in Python originally, but a new 
 125 object of the same type that wraps the same C++ pointer.  If the 
 126 programmer has set additional attributes of that original object they 
 127 will not exist in the new object. 
 129 <p>For a long time now I have wanted to do away with wxPyTypeCast and 
 130 also find a way to return the original Python object from methods like 
 131 FindWindowById.  This project naturally divides into two phases: 
 135 <li>Teach the wrapper methods how to return objects of the right type, 
 136 and be able to then turn wxPyTypeCast in to a no-op. 
 138 <li>Be able to return the original Python shadow object if it still exists. 
 142 <p>The first button below shows the first of these phases (<i>working</i>) 
 143 and the second will show #2 (<i>working as of Python 2.3.2</i>) 
 151 if __name__ 
== '__main__': 
 154     run
.main(['', os
.path
.basename(sys
.argv
[0])])