Mix-in to base events?

Can you explain this on a more basic level? How does delegation work?

--Vicki

···

-----Original Message-----
From: Paul McNett [mailto:p@ulmcnett.com]
Sent: Wednesday, August 18, 2004 3:49 PM
To: wxPython-users@lists.wxwidgets.org
Subject: Re: [wxPython-users] Mix-in to base events?

Chris Barker writes:

One other note: I used delegation (def __getattr__) so that all the
ordinary event attributes are available.

Stanfield, Vicki {D167~Indianapolis} wrote:

One other note: I used delegation (def __getattr__) so that all the ordinary event attributes are available.

Can you explain this on a more basic level? How does delegation work?

Here's some of the code from FloatCanvas:

# create a new event type
EVT_FC_LEFT_DOWN = wx.NewEventType()
...

# create a function for binding the event to a window
# NOTE: I can probably do without this, and use wxWindow.Bind() now.
def EVT_LEFT_DOWN( window, function ):
     window.Connect( -1, -1,EVT_FC_LEFT_DOWN , function )

....

# create a class for all my mouse events:
class _MouseEvent(wx.PyCommandEvent):

"""

This event class takes a regular wxWindows mouse event as a parameter, and wraps it so that there is access to all the original methods. This is similar to subclassing, but you can't subclass a wxWindows event.

The goal is to be able to it just like a regular mouse event.

It adds the method:

GetCoords() , which returns and (x,y) tuple in world coordinates.

Another difference is that it is a CommandEvent, which propagates up the window hierarchy until it is handled.

"""

     def __init__(self, EventType, NativeEvent, WinID, Coords = None):
         wx.PyCommandEvent.__init__(self)

         self.SetEventType( EventType )
         self._NativeEvent = NativeEvent
         self.Coords = Coords

     def GetCoords(self):
         return self.Coords

     # this delegates all other attributes to the native event.
     def __getattr__(self, name):
         return getattr(self._NativeEvent, name)

# To create an instance of the _MouseEvent class:

def _RaiseMouseEvent(self, Event, EventType):
         """
         This is called in various other places to raise a Mouse Event
         """
  # this computes the "World Coordinates" from the mouse coords.
         # This is the whole point of all this, so the user only has to
         # deal with world coordinates.
         pt = self.PixelToWorld( Event.GetPosition() )
         evt = _MouseEvent(EventType, Event, self.GetId(), pt)
         self.GetEventHandler().ProcessEvent(evt)

# this gets called when the native event happens"
def LeftDownEvent(self,event):
      self._RaiseMouseEvent(event,EVT_FC_LEFT_DOWN)

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Chris Barker wrote:

Here's some of the code from FloatCanvas:

# create a new event type
EVT_FC_LEFT_DOWN = wx.NewEventType()
...

For people who just want to create new events and handlers,
also check out wx.lib.newevent.

Roger