X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/0b632977d75f403b7bc043d48a1c15e7394c546c..058f225a44d83d42ba9d773efc705badbf0e5e3c:/include/wx/event.h?ds=sidebyside diff --git a/include/wx/event.h b/include/wx/event.h index 6c77cf5467..db64d8c6c6 100644 --- a/include/wx/event.h +++ b/include/wx/event.h @@ -94,6 +94,11 @@ typedef int wxEventType; // generate a new unique event type extern WXDLLIMPEXP_BASE wxEventType wxNewEventType(); +// FIXME: currently the new events code is disabled because it creates too +// many problems, it should be reenabled a.s.a.p. or removed +#undef wxEVENTS_COMPATIBILITY_2_8 +#define wxEVENTS_COMPATIBILITY_2_8 1 + // macros to create an event type depending on whether type safe events are // enabled. @@ -104,11 +109,11 @@ extern WXDLLIMPEXP_BASE wxEventType wxNewEventType(); #define wxDECLARE_EXPORTED_EVENT( expdecl, name, type ) \ extern const expdecl wxEventType name; - #define wxDEFINE_EVENT_REFERENCE( name, type, value ) \ - const wxEventType &name( value ); + #define wxDEFINE_EVENT_ALIAS( name, type, value ) \ + const wxEventType name = value; - #define wxDECLARE_EXPORTED_EVENT_REFERENCE( expdecl, name, type ) \ - extern const expdecl wxEventType &name; + #define wxDECLARE_EXPORTED_EVENT_ALIAS( expdecl, name, type ) \ + extern const expdecl wxEventType name; #define wxDECLARE_LOCAL_EVENT( name, type ) \ wxDECLARE_EXPORTED_EVENT( wxEMPTY_PARAMETER_VALUE, name, type ) @@ -122,11 +127,11 @@ extern WXDLLIMPEXP_BASE wxEventType wxNewEventType(); #define wxDECLARE_EXPORTED_EVENT( expdecl, name, type ) \ extern const expdecl wxTypedEventType< type > name; - #define wxDEFINE_EVENT_REFERENCE( name, type, value ) \ - const wxTypedEventTypeReference< type > name( value ); + #define wxDEFINE_EVENT_ALIAS( name, type, value ) \ + const wxTypedEventType< type > name( value ); - #define wxDECLARE_EXPORTED_EVENT_REFERENCE( expdecl, name, type ) \ - extern const expdecl wxTypedEventTypeReference< type > name; + #define wxDECLARE_EXPORTED_EVENT_ALIAS( expdecl, name, type ) \ + extern const expdecl wxTypedEventType< type > name; #define wxDECLARE_LOCAL_EVENT( name, type ) \ wxDECLARE_EXPORTED_EVENT( wxEMPTY_PARAMETER_VALUE, name, type ) @@ -154,30 +159,6 @@ private: wxEventType m_type; }; -// Due to a bug in older wx versions wxSpinEvents were being sent with type of -// wxEVT_SCROLL_LINEUP, wxEVT_SCROLL_LINEDOWN and wxEVT_SCROLL_THUMBTRACK. But -// with the type-safe events in place, these event types are associated with -// wxScrollEvent. To allow handling of spin events, new event types have been -// defined in spinbutt.h/spinnbuttcmn.cpp. To maintain backward compatibility -// the spin event types are being initialized with the scroll event types. But -// this presents as with the same static initialization order problem we also -// have for the static event tables. So we use the same solution and the -// template definition below holds a reference to a wxEventType. -template -class WXDLLIMPEXP_BASE wxTypedEventTypeReference -{ -public: - typedef Event CorrespondingEvent; - - wxTypedEventTypeReference(const wxEventType& type) : m_type(type) { } - - // used for static event tables - operator const wxEventType&() const { return m_type; } - -private: - const wxEventType &m_type; -}; - #endif // !wxEVENTS_COMPATIBILITY_2_8 // These are needed for the functor definitions @@ -199,9 +180,12 @@ class WXDLLIMPEXP_BASE wxEventFunctor public: virtual ~wxEventFunctor(); + // this operator is used to actually invoke the event handler virtual void operator()(wxEvtHandler *, wxEvent &) = 0; - virtual bool operator==(const wxEventFunctor& other) const = 0; + // this function tests whether this functor is matched, for the purpose of + // finding it in an event table in Disconnect(), by the given func + virtual bool Matches(const wxEventFunctor& func) const = 0; virtual wxEvtHandler *GetHandler() const { return NULL; } @@ -225,13 +209,12 @@ public: (realHandler->*m_method)(event); } - virtual bool operator==(const wxEventFunctor& other) const + virtual bool Matches(const wxEventFunctor& other) const { wxEvtHandler * const handler = other.GetHandler(); - const wxObjectEventFunction method = other.GetMethod(); return (m_handler == handler || !handler) && - (m_method == method || !method); + (m_method == other.GetMethod()); } virtual wxEvtHandler *GetHandler() const { return m_handler; } @@ -242,6 +225,25 @@ private: wxObjectEventFunction m_method; }; +// Create a functor for the legacy events: handler can be NULL and its default +// value is used by the event table macros + +inline wxObjectEventFunctor * +wxNewEventFunctor(const wxEventType& WXUNUSED(evtType), + wxObjectEventFunction method, + wxEvtHandler *handler = NULL) +{ + return new wxObjectEventFunctor(method, handler); +} + +inline wxObjectEventFunctor +wxConstructEventFunctor(const wxEventType& WXUNUSED(evtType), + wxObjectEventFunction method, + wxEvtHandler *handler) +{ + return wxObjectEventFunctor(method, handler); +} + #if !wxEVENTS_COMPATIBILITY_2_8 template @@ -262,7 +264,7 @@ public: ( *m_handler )( dynamic_cast< typename EventType::CorrespondingEvent & >( event )); } - virtual bool operator == ( const wxEventFunctor &right ) const + virtual bool Matches( const wxEventFunctor &right ) const { wxEventFunctorFunction const &other = dynamic_cast< wxEventFunctorFunction const & >( right ); @@ -307,7 +309,7 @@ public: ( realHandler->*m_method )( dynamic_cast< typename EventType::CorrespondingEvent & >( event )); } - virtual bool operator == ( const wxEventFunctor &right ) const + virtual bool Matches( const wxEventFunctor &right ) const { wxEventFunctorMethod const &other = dynamic_cast< wxEventFunctorMethod const & >( right ); @@ -355,7 +357,7 @@ public: m_functor( dynamic_cast< typename EventType::CorrespondingEvent & >( event )); } - virtual bool operator == ( const wxEventFunctor &right ) const + virtual bool Matches( const wxEventFunctor &right ) const { wxEventFunctorAdapter const &other = dynamic_cast< wxEventFunctorAdapter const & >( right ); @@ -366,28 +368,6 @@ private: Functor m_functor; }; -#endif // wxEVENTS_COMPATIBILITY_2_8 - -// Create a functor for the legacy events: - -inline wxObjectEventFunctor * -wxNewEventFunctor(wxEventType WXUNUSED(evtType), - wxObjectEventFunction method, - wxEvtHandler *handler = NULL) -{ - return new wxObjectEventFunctor(method, handler); -} - -inline wxObjectEventFunctor -wxConstructEventFunctor(wxEventType WXUNUSED(evtType), - wxObjectEventFunction method, - wxEvtHandler *handler = NULL) -{ - return wxObjectEventFunctor(method, handler); -} - -#if !wxEVENTS_COMPATIBILITY_2_8 - // // Create functors for the templatized events (needed in wxEvtHandler::Connect): // @@ -491,6 +471,7 @@ class WXDLLIMPEXP_FWD_CORE wxKeyEvent; class WXDLLIMPEXP_FWD_CORE wxNavigationKeyEvent; class WXDLLIMPEXP_FWD_CORE wxSetCursorEvent; class WXDLLIMPEXP_FWD_CORE wxScrollEvent; +class WXDLLIMPEXP_FWD_CORE wxSpinEvent; class WXDLLIMPEXP_FWD_CORE wxScrollWinEvent; class WXDLLIMPEXP_FWD_CORE wxSizeEvent; class WXDLLIMPEXP_FWD_CORE wxMoveEvent; @@ -588,6 +569,21 @@ wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CORE, wxEVT_SCROLL_THUMBTRACK, wxScrollEven wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CORE, wxEVT_SCROLL_THUMBRELEASE, wxScrollEvent) wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CORE, wxEVT_SCROLL_CHANGED, wxScrollEvent) +// Due to a bug in older wx versions, wxSpinEvents were being sent with type of +// wxEVT_SCROLL_LINEUP, wxEVT_SCROLL_LINEDOWN and wxEVT_SCROLL_THUMBTRACK. But +// with the type-safe events in place, these event types are associated with +// wxScrollEvent. To allow handling of spin events, new event types have been +// defined in spinbutt.h/spinnbuttcmn.cpp. To maintain backward compatibility +// the spin event types are being initialized with the scroll event types. + +#if wxUSE_SPINBTN + +wxDECLARE_EXPORTED_EVENT_ALIAS( WXDLLIMPEXP_CORE, wxEVT_SPIN_UP, wxSpinEvent ) +wxDECLARE_EXPORTED_EVENT_ALIAS( WXDLLIMPEXP_CORE, wxEVT_SPIN_DOWN, wxSpinEvent ) +wxDECLARE_EXPORTED_EVENT_ALIAS( WXDLLIMPEXP_CORE, wxEVT_SPIN, wxSpinEvent ) + +#endif + // Scroll events from wxWindow wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CORE, wxEVT_SCROLLWIN_TOP, wxScrollWinEvent) wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CORE, wxEVT_SCROLLWIN_BOTTOM, wxScrollWinEvent) @@ -714,7 +710,7 @@ public: void Skip(bool skip = true) { m_skipped = skip; } bool GetSkipped() const { return m_skipped; } - // this function is used to create a copy of the event polymorphically and + // This function is used to create a copy of the event polymorphically and // all derived classes must implement it because otherwise wxPostEvent() // for them wouldn't work (it needs to do a copy of the event) virtual wxEvent *Clone() const = 0; @@ -743,6 +739,20 @@ public: m_propagationLevel = propagationLevel; } + + // This is for internal use only and is only called by + // wxEvtHandler::ProcessEvent() to check whether it's the first time this + // event is being processed + bool WasProcessed() + { + if ( m_wasProcessed ) + return true; + + m_wasProcessed = true; + + return false; + } + protected: wxObject* m_eventObject; wxEventType m_eventType; @@ -764,6 +774,12 @@ protected: bool m_skipped; bool m_isCommandEvent; + // initially false but becomes true as soon as WasProcessed() is called for + // the first time, as this is done only by ProcessEvent() it explains the + // variable name: it becomes true after ProcessEvent() was called at least + // once for this event + bool m_wasProcessed; + protected: wxEvent(const wxEvent&); // for implementing Clone() wxEvent& operator=(const wxEvent&); // for derived classes operator=() @@ -1495,7 +1511,7 @@ private: class WXDLLIMPEXP_CORE wxEraseEvent : public wxEvent { public: - wxEraseEvent(int Id = 0, wxDC *dc = (wxDC *) NULL) + wxEraseEvent(int Id = 0, wxDC *dc = NULL) : wxEvent(Id, wxEVT_ERASE_BACKGROUND), m_dc(dc) { } @@ -1890,7 +1906,7 @@ public: wxDropFilesEvent(wxEventType type = wxEVT_NULL, int noFiles = 0, - wxString *files = (wxString *) NULL) + wxString *files = NULL) : wxEvent(0, type), m_noFiles(noFiles), m_pos(), @@ -2123,7 +2139,7 @@ class WXDLLIMPEXP_CORE wxPaletteChangedEvent : public wxEvent public: wxPaletteChangedEvent(wxWindowID winid = 0) : wxEvent(winid, wxEVT_PALETTE_CHANGED), - m_changedWindow((wxWindow *) NULL) + m_changedWindow(NULL) { } wxPaletteChangedEvent(const wxPaletteChangedEvent& event) @@ -2184,7 +2200,7 @@ public: wxNavigationKeyEvent() : wxEvent(0, wxEVT_NAVIGATION_KEY), m_flags(IsForward | FromTab), // defaults are for TAB - m_focus((wxWindow *)NULL) + m_focus(NULL) { m_propagationLevel = wxEVENT_PROPAGATE_NONE; } @@ -2644,14 +2660,27 @@ public: wxEvtHandler(); virtual ~wxEvtHandler(); + + // Event handler chain + // ------------------- + wxEvtHandler *GetNextHandler() const { return m_nextHandler; } wxEvtHandler *GetPreviousHandler() const { return m_previousHandler; } - void SetNextHandler(wxEvtHandler *handler) { m_nextHandler = handler; } - void SetPreviousHandler(wxEvtHandler *handler) { m_previousHandler = handler; } + virtual void SetNextHandler(wxEvtHandler *handler) { m_nextHandler = handler; } + virtual void SetPreviousHandler(wxEvtHandler *handler) { m_previousHandler = handler; } void SetEvtHandlerEnabled(bool enabled) { m_enabled = enabled; } bool GetEvtHandlerEnabled() const { return m_enabled; } + void Unlink(); + bool IsUnlinked() const; + + + + // Event queuing and processing + // ---------------------------- + + // Process an event right now: this can only be called from the main // thread, use QueueEvent() for scheduling the events for // processing from other threads. @@ -2662,6 +2691,7 @@ public: // when called from C code (e.g. in GTK+ callback) when the exception // wouldn't correctly propagate to wxEventLoop. bool SafelyProcessEvent(wxEvent& event); + // NOTE: uses ProcessEvent() // 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 @@ -2684,19 +2714,25 @@ public: } void ProcessPendingEvents(); + // NOTE: uses ProcessEvent() #if wxUSE_THREADS bool ProcessThreadEvent(const wxEvent& event); + // NOTE: uses AddPendingEvent() #endif + + // Connecting and disconnecting + // ---------------------------- + // Dynamic association of a member function handler with the event handler, // winid and event type void Connect(int winid, int lastId, wxEventType eventType, wxObjectEventFunction func, - wxObject *userData = (wxObject *) NULL, - wxEvtHandler *eventSink = (wxEvtHandler *) NULL) + wxObject *userData = NULL, + wxEvtHandler *eventSink = NULL) { wxObjectEventFunctor *functor = wxNewEventFunctor( eventType, func, eventSink ); @@ -2707,23 +2743,23 @@ public: void Connect(int winid, wxEventType eventType, wxObjectEventFunction func, - wxObject *userData = (wxObject *) NULL, - wxEvtHandler *eventSink = (wxEvtHandler *) NULL) + wxObject *userData = NULL, + wxEvtHandler *eventSink = NULL) { Connect(winid, wxID_ANY, eventType, func, userData, eventSink); } // Even more convenient: without id (same as using id of wxID_ANY) void Connect(wxEventType eventType, wxObjectEventFunction func, - wxObject *userData = (wxObject *) NULL, - wxEvtHandler *eventSink = (wxEvtHandler *) NULL) + wxObject *userData = NULL, + wxEvtHandler *eventSink = NULL) { Connect(wxID_ANY, wxID_ANY, eventType, func, userData, eventSink); } bool Disconnect(int winid, int lastId, wxEventType eventType, wxObjectEventFunction func = NULL, - wxObject *userData = (wxObject *) NULL, - wxEvtHandler *eventSink = (wxEvtHandler *) NULL) + wxObject *userData = NULL, + wxEvtHandler *eventSink = NULL) { wxObjectEventFunctor functor = wxConstructEventFunctor( eventType, func, eventSink ); @@ -2733,14 +2769,14 @@ public: bool Disconnect(int winid = wxID_ANY, wxEventType eventType = wxEVT_NULL, wxObjectEventFunction func = NULL, - wxObject *userData = (wxObject *) NULL, - wxEvtHandler *eventSink = (wxEvtHandler *) NULL) + wxObject *userData = NULL, + wxEvtHandler *eventSink = NULL) { return Disconnect(winid, wxID_ANY, eventType, func, userData, eventSink); } bool Disconnect(wxEventType eventType, wxObjectEventFunction func, - wxObject *userData = (wxObject *) NULL, - wxEvtHandler *eventSink = (wxEvtHandler *) NULL) + wxObject *userData = NULL, + wxEvtHandler *eventSink = NULL) { return Disconnect(wxID_ANY, eventType, func, userData, eventSink); } @@ -3059,14 +3095,15 @@ public: // implementation from now on // -------------------------- - // check if the given event table entry matches this event and call the - // handler if it does + // check if the given event table entry matches this event by id (the check + // for the event type should be done by caller) and call the handler if it + // does // // return true if the event was processed, false otherwise (no match or the // handler decided to skip the event) - static bool ProcessEventIfMatches(const wxEventTableEntryBase& tableEntry, - wxEvtHandler *handler, - wxEvent& event); + static bool ProcessEventIfMatchesId(const wxEventTableEntryBase& tableEntry, + wxEvtHandler *handler, + wxEvent& event); virtual bool SearchEventTable(wxEventTable& table, wxEvent& event); bool SearchDynamicEventTable( wxEvent& event ); @@ -3076,12 +3113,7 @@ public: void OnSinkDestroyed( wxEvtHandler *sink ); - // The method processing the event in this event handler (or rather in this - // event handler chain as it also tries the next handler and so on), i.e. - // it returns true if we processed this event or false if we didn't but - // does not call TryParent() in the latter case. It also doesn't call - // wxApp::FilterEvent() before processing it, this is supposed to be done - // by the public ProcessEvent() only once for every event we handle. + // 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