#include "wx/dynarray.h"
#include "wx/thread.h"
#include "wx/tracker.h"
+#include "wx/typeinfo.h"
+#include "wx/any.h"
#ifdef wxHAS_EVENT_BIND
#include "wx/meta/convertible.h"
// generate a new unique event type
extern WXDLLIMPEXP_BASE wxEventType wxNewEventType();
-// New macros to create templatized event types:
-
-#ifndef wxHAS_EVENT_BIND
-
- // Define/Declare a wxEventType-based event type:
+// 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 wxEventType name( wxNewEventType() )
+ 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 wxEventType name
-
- // Define/Declare a wxEventType-based event type and initialize it with a
- // predefined event type. (Only used for wxEVT_SPIN_XXX for backward
- // compatibility)
-
- #define wxDEFINE_EVENT_ALIAS( name, type, value ) \
- const wxEventType name = value
-
- #define wxDECLARE_EXPORTED_EVENT_ALIAS( expdecl, name, type ) \
- extern const expdecl wxEventType name
-
- // Declare a local (not exported) wxEventType-based event 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 )
-#else // wxHAS_EVENT_BIND
-
- // Define/Declare a templatized event type with the corresponding event as
- // a nested typedef:
-
- #define wxDEFINE_EVENT( name, type ) \
- const wxEventTypeTag< type > name( wxNewEventType() )
-
- #define wxDECLARE_EXPORTED_EVENT( expdecl, name, type ) \
- extern const expdecl wxEventTypeTag< type > name
-
- // Define/Declare a templatized event type and initialize it with a
- // predefined event type. (Only used for wxEVT_SPIN_XXX for backward
- // compatibility)
+ // 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
- // Declare a local (not exported) templatized event type:
+ #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 )
-#endif // wxHAS_EVENT_BIND
+ #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:
// compiler we can restore its old definition for it.
typedef wxEventFunction wxObjectEventFunction;
-
-// wxEventFunctorClassInfo is used as a unique identifier for wxEventFunctor-
-// derived classes; it is more light weight than wxClassInfo and can be used in
-// template classes
-typedef void (*wxEventFunctorClassInfo)();
-
-// this macro must be used in wxEventFunctor-derived classes
-#define wxDEFINE_EVENT_FUNCTOR_CLASS_INFO( classInfoName ) \
- static void classInfoName() {}
-
// The event functor which is stored in the static and dynamic event tables:
class WXDLLIMPEXP_BASE wxEventFunctor
{
// finding it in an event table in Unbind(), by the given functor:
virtual bool IsMatching(const wxEventFunctor& functor) const = 0;
- // Test whether the given class info is the same as from this functor. This
- // allows us in IsMatching to safely downcast the given wxEventFunctor without
- // the usage of dynamic_cast<>().
- virtual bool IsSameClass(wxEventFunctorClassInfo classInfo) const = 0;
-
// If the functor holds an wxEvtHandler, then get access to it and track
// its lifetime with wxEventConnectionRef:
virtual wxEvtHandler *GetEvtHandler() const
// wxEventFunction:
virtual wxEventFunction GetEvtMethod() const
{ return NULL; }
+
+private:
+ WX_DECLARE_ABSTRACT_TYPEINFO(wxEventFunctor)
};
// A plain method functor for the untyped legacy event types:
virtual bool IsMatching(const wxEventFunctor& functor) const
{
- if ( functor.IsSameClass( sm_classInfo ))
+ if ( wxTypeId(functor) == wxTypeId(*this) )
{
const wxObjectEventFunctor &other =
static_cast< const wxObjectEventFunctor & >( functor );
return false;
}
- virtual bool IsSameClass( wxEventFunctorClassInfo otherClassInfo ) const
- { return sm_classInfo == otherClassInfo; }
-
virtual wxEvtHandler *GetEvtHandler() const
{ return m_handler; }
wxEvtHandler *m_handler;
wxEventFunction m_method;
- wxDEFINE_EVENT_FUNCTOR_CLASS_INFO( sm_classInfo );
+ // 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()
virtual bool IsMatching(const wxEventFunctor& functor) const
{
- if ( !functor.IsSameClass(sm_classInfo) )
+ if ( wxTypeId(functor) != wxTypeId(*this) )
return false;
typedef wxEventFunctorMethod<EventTag, Class, EventArg, EventHandler>
ThisFunctor;
- // the cast is valid because IsSameClass() returned true above
+ // 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 bool IsSameClass( wxEventFunctorClassInfo otherClassInfo ) const
- { return sm_classInfo == otherClassInfo; }
-
virtual wxEvtHandler *GetEvtHandler() const
{ return this->ConvertToEvtHandler(m_handler); }
EventHandler *m_handler;
void (Class::*m_method)(EventArg&);
- wxDEFINE_EVENT_FUNCTOR_CLASS_INFO( sm_classInfo );
+ // Provide a dummy default ctor for type info purposes
+ wxEventFunctorMethod() { }
+
+ typedef wxEventFunctorMethod<EventTag, Class,
+ EventArg, EventHandler> thisClass;
+ WX_DECLARE_TYPEINFO_INLINE(thisClass)
};
virtual bool IsMatching(const wxEventFunctor &functor) const
{
- if ( !functor.IsSameClass(sm_classInfo) )
+ if ( wxTypeId(functor) != wxTypeId(*this) )
return false;
typedef wxEventFunctorFunction<EventTag, EventArg> ThisFunctor;
return m_handler == other.m_handler;
}
- virtual bool IsSameClass( wxEventFunctorClassInfo otherClassInfo ) const
- { return sm_classInfo == otherClassInfo; }
-
private:
void (*m_handler)(EventArg&);
- wxDEFINE_EVENT_FUNCTOR_CLASS_INFO( sm_classInfo );
+ // Provide a dummy default ctor for type info purposes
+ wxEventFunctorFunction() { }
+
+ typedef wxEventFunctorFunction<EventTag, EventArg> thisClass;
+ WX_DECLARE_TYPEINFO_INLINE(thisClass)
};
virtual bool IsMatching(const wxEventFunctor &functor) const
{
- if ( !functor.IsSameClass(sm_classInfo) )
+ if ( wxTypeId(functor) != wxTypeId(*this) )
return false;
typedef wxEventFunctorFunctor<EventTag, Functor> FunctorThis;
return m_handlerAddr == other.m_handlerAddr;
}
- virtual bool IsSameClass( wxEventFunctorClassInfo otherClassInfo ) const
- { return sm_classInfo == otherClassInfo; }
-
private:
// Store a copy of the functor to prevent using/calling an already
// destroyed instance:
// Use the address of the original functor for comparison in IsMatching:
const void *m_handlerAddr;
- wxDEFINE_EVENT_FUNCTOR_CLASS_INFO( sm_classInfo );
+ // 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 a temporary objects (this
+// 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).
return false;
}
+ // This is also used only internally by ProcessEvent() to check if it
+ // should process the event normally or only restrict the search for the
+ // event handler to this object itself.
+ bool ShouldProcessHereOnly() const { return m_processHereOnly; }
+
protected:
wxObject* m_eventObject;
wxEventType m_eventType;
// once for this event
bool m_wasProcessed;
+ // this flag is used by ProcessEventLocally() to prevent ProcessEvent()
+ // from doing its usual stuff and force it to just call TryHere() instead,
+ // see the comment there explaining why is this needed
+ bool m_processHereOnly;
+
protected:
wxEvent(const wxEvent&); // for implementing Clone()
wxEvent& operator=(const wxEvent&); // for derived classes operator=()
// it needs to access our m_propagationLevel
friend class WXDLLIMPEXP_FWD_BASE wxPropagateOnce;
+ // and this one needs to access our m_processHereOnly
+ friend class WXDLLIMPEXP_FWD_BASE wxEventProcessHereOnly;
+
+
DECLARE_ABSTRACT_CLASS(wxEvent)
};
wxPropagateOnce(wxEvent& event) : m_event(event)
{
wxASSERT_MSG( m_event.m_propagationLevel > 0,
- _T("shouldn't be used unless ShouldPropagate()!") );
+ wxT("shouldn't be used unless ShouldPropagate()!") );
m_event.m_propagationLevel--;
}
wxDECLARE_NO_COPY_CLASS(wxPropagateOnce);
};
+// A helper used by ProcessEventLocally() to restrict the event processing
+// to this handler only.
+class WXDLLIMPEXP_BASE wxEventProcessHereOnly
+{
+public:
+ wxEventProcessHereOnly(wxEvent& event) : m_event(event)
+ {
+ // This would be unexpected and would also restore the wrong value in
+ // this class dtor so if even does happen legitimately we'd need to
+ // store the value in ctor and restore it in dtor.
+ wxASSERT_MSG( !m_event.m_processHereOnly,
+ "shouldn't be used twice for the same event" );
+
+ m_event.m_processHereOnly = true;
+ }
+
+ ~wxEventProcessHereOnly()
+ {
+ m_event.m_processHereOnly = false;
+ }
+
+private:
+ wxEvent& m_event;
+
+ wxDECLARE_NO_COPY_CLASS(wxEventProcessHereOnly);
+};
#if wxUSE_GUI
// make sure our string member (which uses COW, aka refcounting) is not
// shared by other wxString instances:
SetString(GetString().c_str());
+
+#if wxUSE_ANY && (!defined(__VISUALC__) || wxCHECK_VISUALC_VERSION(7))
+ m_payload = event.m_payload;
+#endif
}
virtual wxEvent *Clone() const
{
- wxThreadEvent* ev = new wxThreadEvent(*this);
-
- // make sure our string member (which uses COW, aka refcounting) is not
- // shared by other wxString instances:
- ev->SetString(GetString().c_str());
- return ev;
+ return new wxThreadEvent(*this);
}
// this is important to avoid that calling wxEventLoopBase::YieldFor thread events
virtual wxEventCategory GetEventCategory() const
{ return wxEVT_CATEGORY_THREAD; }
+#if wxUSE_ANY && (!defined(__VISUALC__) || wxCHECK_VISUALC_VERSION(7))
+ template<typename T>
+ void SetPayload(const T& payload)
+ {
+ m_payload = payload;
+ }
+
+ template<typename T>
+ T GetPayload() const
+ {
+ return m_payload.As<T>();
+ }
+
+protected:
+ wxAny m_payload;
+#endif // wxUSE_ANY && (!defined(__VISUALC__) || wxCHECK_VISUALC_VERSION(7))
+
private:
DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxThreadEvent)
};
wxEVT_HOTKEY
*/
+// key categories: the bit flags for IsKeyInCategory() function
+//
+// the enum values used may change in future version of wx
+// use the named constants only, or bitwise combinations thereof
+enum wxKeyCategoryFlags
+{
+ // arrow keys, on and off numeric keypads
+ WXK_CATEGORY_ARROW = 1,
+
+ // page up and page down keys, on and off numeric keypads
+ WXK_CATEGORY_PAGING = 2,
+
+ // home and end keys, on and off numeric keypads
+ WXK_CATEGORY_JUMP = 4,
+
+ // tab key, on and off numeric keypads
+ WXK_CATEGORY_TAB = 8,
+
+ // backspace and delete keys, on and off numeric keypads
+ WXK_CATEGORY_CUT = 16,
+
+ // all keys usually used for navigation
+ WXK_CATEGORY_NAVIGATION = WXK_CATEGORY_ARROW |
+ WXK_CATEGORY_PAGING |
+ WXK_CATEGORY_JUMP
+};
+
class WXDLLIMPEXP_CORE wxKeyEvent : public wxEvent,
public wxKeyboardState
{
// get the key code: an ASCII7 char or an element of wxKeyCode enum
int GetKeyCode() const { return (int)m_keyCode; }
+ // returns true iff this event's key code is of a certain type
+ bool IsKeyInCategory(int category) const;
+
#if wxUSE_UNICODE
// get the Unicode character corresponding to this key
wxChar GetUnicodeKey() const { return m_uniChar; }
// m_loggingOff flag is only used by wxEVT_[QUERY_]END_SESSION, it
// doesn't make sense for wxEVT_CLOSE_WINDOW
wxASSERT_MSG( m_eventType != wxEVT_CLOSE_WINDOW,
- _T("this flag is for end session events only") );
+ wxT("this flag is for end session events only") );
return m_loggingOff;
}
m_lastId(idLast),
m_fn(fn),
m_callbackUserData(data)
- { }
+ {
+ wxASSERT_MSG( idLast == wxID_ANY || winid <= idLast,
+ "invalid IDs range: lower bound > upper bound" );
+ }
wxEventTableEntryBase( const wxEventTableEntryBase &entry )
: m_id( entry.m_id ),
bool SafelyProcessEvent(wxEvent& event);
// NOTE: uses ProcessEvent()
+ // This method tries to process the event in this event handler, including
+ // any preprocessing done by TryBefore() and all the handlers chained to
+ // it, but excluding the post-processing done in TryAfter().
+ //
+ // It is meant to be called from ProcessEvent() only and is not virtual,
+ // additional event handlers can be hooked into the normal event processing
+ // logic using TryBefore() and TryAfter() hooks.
+ //
+ // You can also call it yourself to forward an event to another handler but
+ // without propagating it upwards if it's unhandled (this is usually
+ // unwanted when forwarding as the original handler would already do it if
+ // needed normally).
+ bool ProcessEventLocally(wxEvent& event);
+
// Schedule the given event to be processed later. It takes ownership of
// the event pointer, i.e. it will be deleted later. This is safe to call
// from multiple threads although you still need to ensure that wxString
void OnSinkDestroyed( wxEvtHandler *sink );
- // The method tries to process the event in this event handler.
- //
- // It is meant to be called from ProcessEvent() only and is not virtual,
- // additional event handlers can be hooked into the normal event processing
- // logic using TryBefore() and TryAfter() hooks.
- bool ProcessEventHere(wxEvent& event);
-
-
private:
void DoBind(int winid,
int lastId,
// validators.
virtual bool TryBefore(wxEvent& event);
+ // this one is not a hook but just a helper which looks up the handler in
+ // this object itself called from ProcessEventLocally() and normally
+ // shouldn't be called directly as doing it would ignore any chained event
+ // handlers
+ bool TryHere(wxEvent& event);
+
// this one is called after failing to find the event handle in our own
// table to give a chance to the other windows to process it
//
// pass the event to wxTheApp instance, called from TryAfter()
bool DoTryApp(wxEvent& event);
+ // try to process events in all handlers chained to this one
+ bool DoTryChain(wxEvent& event);
+
DECLARE_DYNAMIC_CLASS_NO_COPY(wxEvtHandler)
};
// (and not in a private header) because the base class must be visible from
// other public headers, please do NOT use this in your code, it will be
// removed from future wx versions without warning.
-#ifndef wxHAS_EVENT_BIND
- #define wxBIND_OR_CONNECT_HACK_BASE_CLASS public wxEvtHandler,
- #define wxBIND_OR_CONNECT_HACK_ONLY_BASE_CLASS : public wxEvtHandler
- #define wxBIND_OR_CONNECT_HACK(w, evt, handler, func, obj) \
- win->Connect(evt, handler(func), NULL, obj)
-#else // wxHAS_EVENT_BIND
+#ifdef wxHAS_EVENT_BIND
#define wxBIND_OR_CONNECT_HACK_BASE_CLASS
#define wxBIND_OR_CONNECT_HACK_ONLY_BASE_CLASS
- #define wxBIND_OR_CONNECT_HACK(w, evt, handler, func, obj) \
+ #define wxBIND_OR_CONNECT_HACK(win, evt, handler, func, obj) \
win->Bind(evt, &func, obj)
+#else // wxHAS_EVENT_BIND
+ #define wxBIND_OR_CONNECT_HACK_BASE_CLASS public wxEvtHandler,
+ #define wxBIND_OR_CONNECT_HACK_ONLY_BASE_CLASS : public wxEvtHandler
+ #define wxBIND_OR_CONNECT_HACK(win, evt, handler, func, obj) \
+ win->Connect(evt, handler(func), NULL, obj)
#endif // wxHAS_EVENT_BIND
#if wxUSE_GUI