diff --git a/include/tool/tool_event.h b/include/tool/tool_event.h index f176db5f16..191336a66c 100644 --- a/include/tool/tool_event.h +++ b/include/tool/tool_event.h @@ -30,10 +30,16 @@ #include #include -#include #include +#ifdef WX_COMPATIBILITY +#include +#else +#include +#endif + + class TOOL_ACTION; class TOOL_MANAGER; @@ -236,26 +242,29 @@ public: return m_actions; } + ///> Returns if it this event has a valid position (true for mouse events) + bool HasPosition() const + { + return m_category == TC_MOUSE; + } + ///> Returns information about difference between current mouse cursor position and the place ///> where dragging has started. - const VECTOR2D& Delta() const + const VECTOR2D Delta() const { - assert( m_category == TC_MOUSE ); // this should be used only with mouse events - return m_mouseDelta; + return returnCheckedPosition( m_mouseDelta ); } ///> Returns mouse cursor position in world coordinates. - const VECTOR2D& Position() const + const VECTOR2D Position() const { - assert( m_category == TC_MOUSE ); // this should be used only with mouse events - return m_mousePos; + return returnCheckedPosition( m_mousePos ); } ///> Returns the point where dragging has started. - const VECTOR2D& DragOrigin() const + const VECTOR2D DragOrigin() const { - assert( m_category == TC_MOUSE ); // this should be used only with mouse events - return m_mouseDragOrigin; + return returnCheckedPosition( m_mouseDragOrigin ); } ///> Returns information about mouse buttons state. @@ -432,6 +441,27 @@ private: m_modifiers = aMods; } + /** + * Ensure that the event is a type that has a position before returning a + * position, otherwise return a mull-constructed position. + * Used to defend the position accessors from runtime access when the event + * does not have a valid position. + * + * @param aPos the position to return if the event is valid + * @return the checked position + */ + VECTOR2D returnCheckedPosition( const VECTOR2D& aPos ) const + { + #ifdef WX_COMPATIBILITY + wxCHECK_MSG( HasPosition(), VECTOR2D(), + "Attempted to get position from non-position event" ); + #else + assert( HasPosition() ); + #endif + + return aPos; + } + TOOL_EVENT_CATEGORY m_category; TOOL_ACTIONS m_actions; TOOL_ACTION_SCOPE m_scope;