mousemanager.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/mousemanager.h
  3. // Purpose: wxMouseEventsManager class declaration
  4. // Author: Vadim Zeitlin
  5. // Created: 2009-04-20
  6. // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
  7. // Licence: wxWindows licence
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_MOUSEMANAGER_H_
  10. #define _WX_MOUSEMANAGER_H_
  11. #include "wx/event.h"
  12. // ----------------------------------------------------------------------------
  13. // wxMouseEventsManager
  14. // ----------------------------------------------------------------------------
  15. /*
  16. This class handles mouse events and synthesizes high-level notifications
  17. such as clicks and drag events from low level mouse button presses and
  18. mouse movement events. It is useful because handling the mouse events is
  19. less obvious than might seem at a first glance: for example, clicks on an
  20. object should only be generated if the mouse was both pressed and released
  21. over it and not just released (so it requires storing the previous state)
  22. and dragging shouldn't start before the mouse moves away far enough.
  23. This class encapsulates all these dull details for controls containing
  24. multiple items which can be identified by a positive integer index and you
  25. just need to implement its pure virtual functions to use it.
  26. */
  27. class WXDLLIMPEXP_CORE wxMouseEventsManager : public wxEvtHandler
  28. {
  29. public:
  30. // a mouse event manager is always associated with a window and must be
  31. // deleted by the window when it is destroyed so if it is created using the
  32. // default ctor Create() must be called later
  33. wxMouseEventsManager() { Init(); }
  34. wxMouseEventsManager(wxWindow *win) { Init(); Create(win); }
  35. bool Create(wxWindow *win);
  36. virtual ~wxMouseEventsManager();
  37. protected:
  38. // called to find the item at the given position: return wxNOT_FOUND (-1)
  39. // if there is no item here
  40. virtual int MouseHitTest(const wxPoint& pos) = 0;
  41. // called when the user clicked (i.e. pressed and released mouse over the
  42. // same item), should normally generate a notification about this click and
  43. // return true if it was handled or false otherwise, determining whether
  44. // the original mouse event is skipped or not
  45. virtual bool MouseClicked(int item) = 0;
  46. // called to start dragging the given item, should generate the appropriate
  47. // BEGIN_DRAG event and return false if dragging this item was forbidden
  48. virtual bool MouseDragBegin(int item, const wxPoint& pos) = 0;
  49. // called while the item is being dragged, should normally update the
  50. // feedback on screen (usually using wxOverlay)
  51. virtual void MouseDragging(int item, const wxPoint& pos) = 0;
  52. // called when the mouse is released after dragging the item
  53. virtual void MouseDragEnd(int item, const wxPoint& pos) = 0;
  54. // called when mouse capture is lost while dragging the item, should remove
  55. // the visual feedback drawn by MouseDragging()
  56. virtual void MouseDragCancelled(int item) = 0;
  57. // you don't need to override those but you will want to do if it your
  58. // control renders pressed items differently
  59. // called when the item is becomes pressed, can be used to change its
  60. // appearance
  61. virtual void MouseClickBegin(int WXUNUSED(item)) { }
  62. // called if the mouse capture was lost while the item was pressed, can be
  63. // used to restore the default item appearance if it was changed in
  64. // MouseClickBegin()
  65. virtual void MouseClickCancelled(int WXUNUSED(item)) { }
  66. private:
  67. /*
  68. Here is a small diagram explaining the switches between different
  69. states:
  70. /---------->NORMAL<--------------- Drag end
  71. / / / | event
  72. / / | | ^
  73. / / | | |
  74. Click / N | | mouse | mouse up
  75. event / | | down |
  76. | / | | DRAGGING
  77. | / | | ^
  78. Y|/ N \ v |Y
  79. +-------------+ +--------+ N +-----------+
  80. |On same item?| |On item?| -----------|Begin drag?|
  81. +-------------+ +--------+ / +-----------+
  82. ^ | / ^
  83. | | / |
  84. \ mouse | / mouse moved |
  85. \ up v v far enough /
  86. \--------PRESSED-------------------/
  87. There are also transitions from PRESSED and DRAGGING to NORMAL in case
  88. the mouse capture is lost or Escape key is pressed which are not shown.
  89. */
  90. enum State
  91. {
  92. State_Normal, // initial, default state
  93. State_Pressed, // mouse was pressed over an item
  94. State_Dragging // the item is being dragged
  95. };
  96. // common part of both ctors
  97. void Init();
  98. // various event handlers
  99. void OnCaptureLost(wxMouseCaptureLostEvent& event);
  100. void OnLeftDown(wxMouseEvent& event);
  101. void OnLeftUp(wxMouseEvent& event);
  102. void OnMove(wxMouseEvent& event);
  103. // the associated window, never NULL except between the calls to the
  104. // default ctor and Create()
  105. wxWindow *m_win;
  106. // the current state
  107. State m_state;
  108. // the details of the operation currently in progress, only valid if
  109. // m_state is not normal
  110. // the item being pressed or dragged (always valid, i.e. != wxNOT_FOUND if
  111. // m_state != State_Normal)
  112. int m_item;
  113. // the position of the last mouse event of interest: either mouse press in
  114. // State_Pressed or last movement event in State_Dragging
  115. wxPoint m_posLast;
  116. DECLARE_EVENT_TABLE()
  117. wxDECLARE_NO_COPY_CLASS(wxMouseEventsManager);
  118. };
  119. #endif // _WX_MOUSEMANAGER_H_