+// define macros to create new event types:
+#ifdef wxHAS_EVENT_BIND
+ // events are represented by an instance of wxEventTypeTag and the
+ // corresponding type must be specified for type-safety checks
+
+ // define a new custom event type, can be used alone or after event
+ // declaration in the header using one of the macros below
+ #define wxDEFINE_EVENT( name, type ) \
+ const wxEventTypeTag< type > name( wxNewEventType() )
+
+ // the general version allowing exporting the event type from DLL, used by
+ // wxWidgets itself
+ #define wxDECLARE_EXPORTED_EVENT( expdecl, name, type ) \
+ extern const expdecl wxEventTypeTag< type > name
+
+ // this is the version which will normally be used in the user code
+ #define wxDECLARE_EVENT( name, type ) \
+ wxDECLARE_EXPORTED_EVENT( wxEMPTY_PARAMETER_VALUE, name, type )
+
+
+ // these macros are only used internally for backwards compatibility and
+ // allow to define an alias for an existing event type (this is used by
+ // wxEVT_SPIN_XXX)
+ #define wxDEFINE_EVENT_ALIAS( name, type, value ) \
+ const wxEventTypeTag< type > name( value )
+
+ #define wxDECLARE_EXPORTED_EVENT_ALIAS( expdecl, name, type ) \
+ extern const expdecl wxEventTypeTag< type > name
+#else // !wxHAS_EVENT_BIND
+ // the macros are the same ones as above but defined differently as we only
+ // use the integer event type values to identify events in this case
+
+ #define wxDEFINE_EVENT( name, type ) \
+ const wxEventType name( wxNewEventType() )
+
+ #define wxDECLARE_EXPORTED_EVENT( expdecl, name, type ) \
+ extern const expdecl wxEventType name
+ #define wxDECLARE_EVENT( name, type ) \
+ wxDECLARE_EXPORTED_EVENT( wxEMPTY_PARAMETER_VALUE, name, type )
+
+ #define wxDEFINE_EVENT_ALIAS( name, type, value ) \
+ const wxEventType name = value
+ #define wxDECLARE_EXPORTED_EVENT_ALIAS( expdecl, name, type ) \
+ extern const expdecl wxEventType name
+#endif // wxHAS_EVENT_BIND/!wxHAS_EVENT_BIND
+
+// Try to cast the given event handler to the correct handler type:
+
+#define wxEVENT_HANDLER_CAST( functype, func ) \
+ ( wxObjectEventFunction )( wxEventFunction )wxStaticCastEvent( functype, &func )
+
+
+#ifdef wxHAS_EVENT_BIND
+
+// The tag is a type associated to the event type (which is an integer itself,
+// in spite of its name) value. It exists in order to be used as a template
+// parameter and provide a mapping between the event type values and their
+// corresponding wxEvent-derived classes.
+template <typename T>
+class wxEventTypeTag
+{
+public:
+ // The class of wxEvent-derived class carried by the events of this type.
+ typedef T EventClass;
+
+ wxEventTypeTag(wxEventType type) { m_type = type; }
+
+ // Return a wxEventType reference for the initialization of the static
+ // event tables. See wxEventTableEntry::m_eventType for a more thorough
+ // explanation.
+ operator const wxEventType&() const { return m_type; }
+
+private:
+ wxEventType m_type;
+};
+
+#endif // wxHAS_EVENT_BIND
+
+// These are needed for the functor definitions
+typedef void (wxEvtHandler::*wxEventFunction)(wxEvent&);
+
+// We had some trouble (specifically with eVC for ARM WinCE build) with using
+// wxEventFunction in the past so we had introduced wxObjectEventFunction which
+// used to be a typedef for a member of wxObject and not wxEvtHandler to work
+// around this but as eVC is not really supported any longer we now only keep
+// this for backwards compatibility and, despite its name, this is a typedef
+// for wxEvtHandler member now -- but if we have the same problem with another
+// compiler we can restore its old definition for it.
+typedef wxEventFunction wxObjectEventFunction;
+
+// The event functor which is stored in the static and dynamic event tables:
+class WXDLLIMPEXP_BASE wxEventFunctor
+{
+public:
+ virtual ~wxEventFunctor();
+
+ // Invoke the actual event handler:
+ virtual void operator()(wxEvtHandler *, wxEvent&) = 0;
+
+ // this function tests whether this functor is matched, for the purpose of
+ // finding it in an event table in Unbind(), by the given functor:
+ virtual bool IsMatching(const wxEventFunctor& functor) const = 0;
+
+ // If the functor holds an wxEvtHandler, then get access to it and track
+ // its lifetime with wxEventConnectionRef:
+ virtual wxEvtHandler *GetEvtHandler() const
+ { return NULL; }
+
+ // This is only used to maintain backward compatibility in
+ // wxAppConsoleBase::CallEventHandler and ensures that an overwritten
+ // wxAppConsoleBase::HandleEvent is still called for functors which hold an
+ // wxEventFunction:
+ virtual wxEventFunction GetEvtMethod() const
+ { return NULL; }
+
+private:
+ WX_DECLARE_ABSTRACT_TYPEINFO(wxEventFunctor)
+};
+
+// A plain method functor for the untyped legacy event types:
+class WXDLLIMPEXP_BASE wxObjectEventFunctor : public wxEventFunctor
+{
+public:
+ wxObjectEventFunctor(wxObjectEventFunction method, wxEvtHandler *handler)
+ : m_handler( handler ), m_method( method )
+ { }
+
+ virtual void operator()(wxEvtHandler *handler, wxEvent& event);
+
+ virtual bool IsMatching(const wxEventFunctor& functor) const
+ {
+ if ( wxTypeId(functor) == wxTypeId(*this) )
+ {
+ const wxObjectEventFunctor &other =
+ static_cast< const wxObjectEventFunctor & >( functor );
+
+ // FIXME-VC6: amazing but true: replacing "method == NULL" here
+ // with "!method" makes VC6 crash with an ICE in DLL build (only!)
+
+ return ( m_method == other.m_method || other.m_method == NULL ) &&
+ ( m_handler == other.m_handler || other.m_handler == NULL );
+ }
+ else
+ return false;
+ }
+
+ virtual wxEvtHandler *GetEvtHandler() const
+ { return m_handler; }
+
+ virtual wxEventFunction GetEvtMethod() const
+ { return m_method; }
+
+private:
+ wxEvtHandler *m_handler;
+ wxEventFunction m_method;
+
+ // Provide a dummy default ctor for type info purposes
+ wxObjectEventFunctor() { }
+
+ WX_DECLARE_TYPEINFO_INLINE(wxObjectEventFunctor)
+};
+
+// Create a functor for the legacy events: used by Connect()
+inline wxObjectEventFunctor *
+wxNewEventFunctor(const wxEventType& WXUNUSED(evtType),
+ wxObjectEventFunction method,
+ wxEvtHandler *handler)
+{
+ return new wxObjectEventFunctor(method, handler);
+}
+
+// This version is used by wxDECLARE_EVENT_TABLE_ENTRY()
+inline wxObjectEventFunctor *
+wxNewEventTableFunctor(const wxEventType& WXUNUSED(evtType),
+ wxObjectEventFunction method)
+{
+ return new wxObjectEventFunctor(method, NULL);
+}
+
+inline wxObjectEventFunctor
+wxMakeEventFunctor(const wxEventType& WXUNUSED(evtType),
+ wxObjectEventFunction method,
+ wxEvtHandler *handler)
+{
+ return wxObjectEventFunctor(method, handler);
+}
+
+#ifdef wxHAS_EVENT_BIND
+
+namespace wxPrivate
+{
+
+// helper template defining nested "type" typedef as the event class
+// corresponding to the given event type
+template <typename T> struct EventClassOf;
+
+// the typed events provide the information about the class of the events they
+// carry themselves:
+template <typename T>
+struct EventClassOf< wxEventTypeTag<T> >
+{
+ typedef typename wxEventTypeTag<T>::EventClass type;
+};
+
+// for the old untyped events we don't have information about the exact event
+// class carried by them
+template <>
+struct EventClassOf<wxEventType>
+{
+ typedef wxEvent type;
+};
+
+
+// helper class defining operations different for method functors using an
+// object of wxEvtHandler-derived class as handler and the others
+template <typename T, typename A, bool> struct HandlerImpl;
+
+// specialization for handlers deriving from wxEvtHandler
+template <typename T, typename A>
+struct HandlerImpl<T, A, true>
+{
+ static bool IsEvtHandler()
+ { return true; }
+ static T *ConvertFromEvtHandler(wxEvtHandler *p)
+ { return static_cast<T *>(p); }
+ static wxEvtHandler *ConvertToEvtHandler(T *p)
+ { return p; }
+ static wxEventFunction ConvertToEvtMethod(void (T::*f)(A&))
+ { return static_cast<wxEventFunction>(
+ reinterpret_cast<void (T::*)(wxEvent&)>(f)); }
+};
+
+// specialization for handlers not deriving from wxEvtHandler
+template <typename T, typename A>
+struct HandlerImpl<T, A, false>
+{
+ static bool IsEvtHandler()
+ { return false; }
+ static T *ConvertFromEvtHandler(wxEvtHandler *)
+ { return NULL; }
+ static wxEvtHandler *ConvertToEvtHandler(T *)
+ { return NULL; }
+ static wxEventFunction ConvertToEvtMethod(void (T::*)(A&))
+ { return NULL; }
+};
+
+} // namespace wxPrivate
+
+// functor forwarding the event to a method of the given object
+//
+// notice that the object class may be different from the class in which the
+// method is defined but it must be convertible to this class
+//
+// also, the type of the handler parameter doesn't need to be exactly the same
+// as EventTag::EventClass but it must be its base class -- this is explicitly
+// allowed to handle different events in the same handler taking wxEvent&, for
+// example
+template
+ <typename EventTag, typename Class, typename EventArg, typename EventHandler>
+class wxEventFunctorMethod
+ : public wxEventFunctor,
+ private wxPrivate::HandlerImpl
+ <
+ Class,
+ EventArg,
+ wxConvertibleTo<Class, wxEvtHandler>::value != 0
+ >
+{
+private:
+ static void CheckHandlerArgument(EventArg *) { }
+
+public:
+ // the event class associated with the given event tag
+ typedef typename wxPrivate::EventClassOf<EventTag>::type EventClass;
+
+
+ wxEventFunctorMethod(void (Class::*method)(EventArg&), EventHandler *handler)
+ : m_handler( handler ), m_method( method )
+ {
+ wxASSERT_MSG( handler || this->IsEvtHandler(),
+ "handlers defined in non-wxEvtHandler-derived classes "
+ "must be connected with a valid sink object" );
+
+ // if you get an error here it means that the signature of the handler
+ // you're trying to use is not compatible with (i.e. is not the same as
+ // or a base class of) the real event class used for this event type
+ CheckHandlerArgument(static_cast<EventClass *>(NULL));
+ }
+
+ virtual void operator()(wxEvtHandler *handler, wxEvent& event)
+ {
+ Class * realHandler = m_handler;
+ if ( !realHandler )
+ {
+ realHandler = this->ConvertFromEvtHandler(handler);
+
+ // this is not supposed to happen but check for it nevertheless
+ wxCHECK_RET( realHandler, "invalid event handler" );
+ }
+
+ // the real (run-time) type of event is EventClass and we checked in
+ // the ctor that EventClass can be converted to EventArg, so this cast
+ // is always valid
+ (realHandler->*m_method)(static_cast<EventArg&>(event));
+ }
+
+ virtual bool IsMatching(const wxEventFunctor& functor) const
+ {
+ if ( wxTypeId(functor) != wxTypeId(*this) )
+ return false;
+
+ typedef wxEventFunctorMethod<EventTag, Class, EventArg, EventHandler>
+ ThisFunctor;
+
+ // the cast is valid because wxTypeId()s matched above
+ const ThisFunctor& other = static_cast<const ThisFunctor &>(functor);
+
+ return (m_method == other.m_method || other.m_method == NULL) &&
+ (m_handler == other.m_handler || other.m_handler == NULL);
+ }
+
+ virtual wxEvtHandler *GetEvtHandler() const
+ { return this->ConvertToEvtHandler(m_handler); }
+
+ virtual wxEventFunction GetEvtMethod() const
+ { return this->ConvertToEvtMethod(m_method); }
+
+private:
+ EventHandler *m_handler;
+ void (Class::*m_method)(EventArg&);
+
+ // Provide a dummy default ctor for type info purposes
+ wxEventFunctorMethod() { }
+
+ typedef wxEventFunctorMethod<EventTag, Class,
+ EventArg, EventHandler> thisClass;
+ WX_DECLARE_TYPEINFO_INLINE(thisClass)
+};
+
+
+// functor forwarding the event to function (function, static method)
+template <typename EventTag, typename EventArg>
+class wxEventFunctorFunction : public wxEventFunctor
+{
+private:
+ static void CheckHandlerArgument(EventArg *) { }
+
+public:
+ // the event class associated with the given event tag
+ typedef typename wxPrivate::EventClassOf<EventTag>::type EventClass;
+
+ wxEventFunctorFunction( void ( *handler )( EventArg & ))
+ : m_handler( handler )
+ {
+ // if you get an error here it means that the signature of the handler
+ // you're trying to use is not compatible with (i.e. is not the same as
+ // or a base class of) the real event class used for this event type
+ CheckHandlerArgument(static_cast<EventClass *>(NULL));
+ }
+
+ virtual void operator()(wxEvtHandler *WXUNUSED(handler), wxEvent& event)
+ {
+ // If you get an error here like "must use .* or ->* to call
+ // pointer-to-member function" then you probably tried to call
+ // Bind/Unbind with a method pointer but without a handler pointer or
+ // NULL as a handler e.g.:
+ // Unbind( wxEVT_XXX, &EventHandler::method );
+ // or
+ // Unbind( wxEVT_XXX, &EventHandler::method, NULL )
+ m_handler(static_cast<EventArg&>(event));
+ }
+
+ virtual bool IsMatching(const wxEventFunctor &functor) const
+ {
+ if ( wxTypeId(functor) != wxTypeId(*this) )
+ return false;
+
+ typedef wxEventFunctorFunction<EventTag, EventArg> ThisFunctor;
+
+ const ThisFunctor& other = static_cast<const ThisFunctor&>( functor );
+
+ return m_handler == other.m_handler;
+ }
+
+private:
+ void (*m_handler)(EventArg&);
+
+ // Provide a dummy default ctor for type info purposes
+ wxEventFunctorFunction() { }
+
+ typedef wxEventFunctorFunction<EventTag, EventArg> thisClass;
+ WX_DECLARE_TYPEINFO_INLINE(thisClass)
+};
+
+
+template <typename EventTag, typename Functor>
+class wxEventFunctorFunctor : public wxEventFunctor
+{
+public:
+ typedef typename EventTag::EventClass EventArg;
+
+ wxEventFunctorFunctor(const Functor& handler)
+ : m_handler(handler), m_handlerAddr(&handler)
+ { }
+
+ virtual void operator()(wxEvtHandler *WXUNUSED(handler), wxEvent& event)
+ {
+ // If you get an error here like "must use '.*' or '->*' to call
+ // pointer-to-member function" then you probably tried to call
+ // Bind/Unbind with a method pointer but without a handler pointer or
+ // NULL as a handler e.g.:
+ // Unbind( wxEVT_XXX, &EventHandler::method );
+ // or
+ // Unbind( wxEVT_XXX, &EventHandler::method, NULL )
+ m_handler(static_cast<EventArg&>(event));
+ }
+
+ virtual bool IsMatching(const wxEventFunctor &functor) const
+ {
+ if ( wxTypeId(functor) != wxTypeId(*this) )
+ return false;
+
+ typedef wxEventFunctorFunctor<EventTag, Functor> FunctorThis;
+
+ const FunctorThis& other = static_cast<const FunctorThis&>(functor);
+
+ // The only reliable/portable way to compare two functors is by
+ // identity:
+ return m_handlerAddr == other.m_handlerAddr;
+ }
+
+private:
+ // Store a copy of the functor to prevent using/calling an already
+ // destroyed instance:
+ Functor m_handler;
+
+ // Use the address of the original functor for comparison in IsMatching:
+ const void *m_handlerAddr;
+
+ // Provide a dummy default ctor for type info purposes
+ wxEventFunctorFunctor() { }
+
+ typedef wxEventFunctorFunctor<EventTag, Functor> thisClass;
+ WX_DECLARE_TYPEINFO_INLINE(thisClass)
+};
+
+// Create functors for the templatized events, either allocated on the heap for
+// wxNewXXX() variants (this is needed in wxEvtHandler::Bind<>() to store them
+// in dynamic event table) or just by returning them as temporary objects (this
+// is enough for Unbind<>() and we avoid unnecessary heap allocation like this).
+
+
+// Create functors wrapping functions:
+template <typename EventTag, typename EventArg>
+inline wxEventFunctorFunction<EventTag, EventArg> *
+wxNewEventFunctor(const EventTag&, void (*func)(EventArg &))
+{
+ return new wxEventFunctorFunction<EventTag, EventArg>(func);
+}
+
+template <typename EventTag, typename EventArg>
+inline wxEventFunctorFunction<EventTag, EventArg>
+wxMakeEventFunctor(const EventTag&, void (*func)(EventArg &))
+{
+ return wxEventFunctorFunction<EventTag, EventArg>(func);
+}
+
+// Create functors wrapping other functors:
+template <typename EventTag, typename Functor>
+inline wxEventFunctorFunctor<EventTag, Functor> *
+wxNewEventFunctor(const EventTag&, const Functor &func)
+{
+ return new wxEventFunctorFunctor<EventTag, Functor>(func);
+}
+
+template <typename EventTag, typename Functor>
+inline wxEventFunctorFunctor<EventTag, Functor>
+wxMakeEventFunctor(const EventTag&, const Functor &func)
+{
+ return wxEventFunctorFunctor<EventTag, Functor>(func);
+}
+
+// Create functors wrapping methods:
+template
+ <typename EventTag, typename Class, typename EventArg, typename EventHandler>
+inline wxEventFunctorMethod<EventTag, Class, EventArg, EventHandler> *
+wxNewEventFunctor(const EventTag&,
+ void (Class::*method)(EventArg&),
+ EventHandler *handler)
+{
+ return new wxEventFunctorMethod<EventTag, Class, EventArg, EventHandler>(
+ method, handler);
+}
+
+template
+ <typename EventTag, typename Class, typename EventArg, typename EventHandler>
+inline wxEventFunctorMethod<EventTag, Class, EventArg, EventHandler>
+wxMakeEventFunctor(const EventTag&,
+ void (Class::*method)(EventArg&),
+ EventHandler *handler)
+{
+ return wxEventFunctorMethod<EventTag, Class, EventArg, EventHandler>(
+ method, handler);
+}
+
+// Create an event functor for the event table via wxDECLARE_EVENT_TABLE_ENTRY:
+// in this case we don't have the handler (as it's always the same as the
+// object which generated the event) so we must use Class as its type
+template <typename EventTag, typename Class, typename EventArg>
+inline wxEventFunctorMethod<EventTag, Class, EventArg, Class> *
+wxNewEventTableFunctor(const EventTag&, void (Class::*method)(EventArg&))
+{
+ return new wxEventFunctorMethod<EventTag, Class, EventArg, Class>(
+ method, NULL);
+}
+
+#endif // wxHAS_EVENT_BIND
+
+