The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

361 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef __JUCE_MOUSEEVENT_JUCEHEADER__
  18. #define __JUCE_MOUSEEVENT_JUCEHEADER__
  19. class Component;
  20. class MouseInputSource;
  21. #include "../keyboard/juce_ModifierKeys.h"
  22. //==============================================================================
  23. /**
  24. Contains position and status information about a mouse event.
  25. @see MouseListener, Component::mouseMove, Component::mouseEnter, Component::mouseExit,
  26. Component::mouseDown, Component::mouseUp, Component::mouseDrag
  27. */
  28. class JUCE_API MouseEvent
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates a MouseEvent.
  33. Normally an application will never need to use this.
  34. @param source the source that's invoking the event
  35. @param position the position of the mouse, relative to the component that is passed-in
  36. @param modifiers the key modifiers at the time of the event
  37. @param eventComponent the component that the mouse event applies to
  38. @param originator the component that originally received the event
  39. @param eventTime the time the event happened
  40. @param mouseDownPos the position of the corresponding mouse-down event (relative to the component that is passed-in).
  41. If there isn't a corresponding mouse-down (e.g. for a mouse-move), this will just be
  42. the same as the current mouse-x position.
  43. @param mouseDownTime the time at which the corresponding mouse-down event happened
  44. If there isn't a corresponding mouse-down (e.g. for a mouse-move), this will just be
  45. the same as the current mouse-event time.
  46. @param numberOfClicks how many clicks, e.g. a double-click event will be 2, a triple-click will be 3, etc
  47. @param mouseWasDragged whether the mouse has been dragged significantly since the previous mouse-down
  48. */
  49. MouseEvent (MouseInputSource& source,
  50. Point<int> position,
  51. ModifierKeys modifiers,
  52. Component* eventComponent,
  53. Component* originator,
  54. Time eventTime,
  55. Point<int> mouseDownPos,
  56. Time mouseDownTime,
  57. int numberOfClicks,
  58. bool mouseWasDragged) noexcept;
  59. /** Destructor. */
  60. ~MouseEvent() noexcept;
  61. //==============================================================================
  62. /** The x-position of the mouse when the event occurred.
  63. This value is relative to the top-left of the component to which the
  64. event applies (as indicated by the MouseEvent::eventComponent field).
  65. */
  66. const int x;
  67. /** The y-position of the mouse when the event occurred.
  68. This value is relative to the top-left of the component to which the
  69. event applies (as indicated by the MouseEvent::eventComponent field).
  70. */
  71. const int y;
  72. /** The key modifiers associated with the event.
  73. This will let you find out which mouse buttons were down, as well as which
  74. modifier keys were held down.
  75. When used for mouse-up events, this will indicate the state of the mouse buttons
  76. just before they were released, so that you can tell which button they let go of.
  77. */
  78. const ModifierKeys mods;
  79. /** The component that this event applies to.
  80. This is usually the component that the mouse was over at the time, but for mouse-drag
  81. events the mouse could actually be over a different component and the events are
  82. still sent to the component that the button was originally pressed on.
  83. The x and y member variables are relative to this component's position.
  84. If you use getEventRelativeTo() to retarget this object to be relative to a different
  85. component, this pointer will be updated, but originalComponent remains unchanged.
  86. @see originalComponent
  87. */
  88. Component* const eventComponent;
  89. /** The component that the event first occurred on.
  90. If you use getEventRelativeTo() to retarget this object to be relative to a different
  91. component, this value remains unchanged to indicate the first component that received it.
  92. @see eventComponent
  93. */
  94. Component* const originalComponent;
  95. /** The time that this mouse-event occurred. */
  96. const Time eventTime;
  97. /** The time that the corresponding mouse-down event occurred. */
  98. const Time mouseDownTime;
  99. /** The source device that generated this event. */
  100. MouseInputSource& source;
  101. //==============================================================================
  102. /** Returns the x co-ordinate of the last place that a mouse was pressed.
  103. The co-ordinate is relative to the component specified in MouseEvent::component.
  104. @see getDistanceFromDragStart, getDistanceFromDragStartX, mouseWasClicked
  105. */
  106. int getMouseDownX() const noexcept;
  107. /** Returns the y co-ordinate of the last place that a mouse was pressed.
  108. The co-ordinate is relative to the component specified in MouseEvent::component.
  109. @see getDistanceFromDragStart, getDistanceFromDragStartX, mouseWasClicked
  110. */
  111. int getMouseDownY() const noexcept;
  112. /** Returns the co-ordinates of the last place that a mouse was pressed.
  113. The co-ordinates are relative to the component specified in MouseEvent::component.
  114. @see getDistanceFromDragStart, getDistanceFromDragStartX, mouseWasClicked
  115. */
  116. Point<int> getMouseDownPosition() const noexcept;
  117. /** Returns the straight-line distance between where the mouse is now and where it
  118. was the last time the button was pressed.
  119. This is quite handy for things like deciding whether the user has moved far enough
  120. for it to be considered a drag operation.
  121. @see getDistanceFromDragStartX
  122. */
  123. int getDistanceFromDragStart() const noexcept;
  124. /** Returns the difference between the mouse's current x postion and where it was
  125. when the button was last pressed.
  126. @see getDistanceFromDragStart
  127. */
  128. int getDistanceFromDragStartX() const noexcept;
  129. /** Returns the difference between the mouse's current y postion and where it was
  130. when the button was last pressed.
  131. @see getDistanceFromDragStart
  132. */
  133. int getDistanceFromDragStartY() const noexcept;
  134. /** Returns the difference between the mouse's current postion and where it was
  135. when the button was last pressed.
  136. @see getDistanceFromDragStart
  137. */
  138. Point<int> getOffsetFromDragStart() const noexcept;
  139. /** Returns true if the mouse has just been clicked.
  140. Used in either your mouseUp() or mouseDrag() methods, this will tell you whether
  141. the user has dragged the mouse more than a few pixels from the place where the
  142. mouse-down occurred.
  143. Once they have dragged it far enough for this method to return false, it will continue
  144. to return false until the mouse-up, even if they move the mouse back to the same
  145. position where they originally pressed it. This means that it's very handy for
  146. objects that can either be clicked on or dragged, as you can use it in the mouseDrag()
  147. callback to ignore any small movements they might make while clicking.
  148. @returns true if the mouse wasn't dragged by more than a few pixels between
  149. the last time the button was pressed and released.
  150. */
  151. bool mouseWasClicked() const noexcept;
  152. /** For a click event, the number of times the mouse was clicked in succession.
  153. So for example a double-click event will return 2, a triple-click 3, etc.
  154. */
  155. int getNumberOfClicks() const noexcept { return numberOfClicks; }
  156. /** Returns the time that the mouse button has been held down for.
  157. If called from a mouseDrag or mouseUp callback, this will return the
  158. number of milliseconds since the corresponding mouseDown event occurred.
  159. If called in other contexts, e.g. a mouseMove, then the returned value
  160. may be 0 or an undefined value.
  161. */
  162. int getLengthOfMousePress() const noexcept;
  163. //==============================================================================
  164. /** The position of the mouse when the event occurred.
  165. This position is relative to the top-left of the component to which the
  166. event applies (as indicated by the MouseEvent::eventComponent field).
  167. */
  168. Point<int> getPosition() const noexcept;
  169. /** Returns the mouse x position of this event, in global screen co-ordinates.
  170. The co-ordinates are relative to the top-left of the main monitor.
  171. @see getScreenPosition
  172. */
  173. int getScreenX() const;
  174. /** Returns the mouse y position of this event, in global screen co-ordinates.
  175. The co-ordinates are relative to the top-left of the main monitor.
  176. @see getScreenPosition
  177. */
  178. int getScreenY() const;
  179. /** Returns the mouse position of this event, in global screen co-ordinates.
  180. The co-ordinates are relative to the top-left of the main monitor.
  181. @see getMouseDownScreenPosition
  182. */
  183. Point<int> getScreenPosition() const;
  184. /** Returns the x co-ordinate at which the mouse button was last pressed.
  185. The co-ordinates are relative to the top-left of the main monitor.
  186. @see getMouseDownScreenPosition
  187. */
  188. int getMouseDownScreenX() const;
  189. /** Returns the y co-ordinate at which the mouse button was last pressed.
  190. The co-ordinates are relative to the top-left of the main monitor.
  191. @see getMouseDownScreenPosition
  192. */
  193. int getMouseDownScreenY() const;
  194. /** Returns the co-ordinates at which the mouse button was last pressed.
  195. The co-ordinates are relative to the top-left of the main monitor.
  196. @see getScreenPosition
  197. */
  198. Point<int> getMouseDownScreenPosition() const;
  199. //==============================================================================
  200. /** Creates a version of this event that is relative to a different component.
  201. The x and y positions of the event that is returned will have been
  202. adjusted to be relative to the new component.
  203. The component pointer that is passed-in must not be null.
  204. */
  205. MouseEvent getEventRelativeTo (Component* newComponent) const noexcept;
  206. /** Creates a copy of this event with a different position.
  207. All other members of the event object are the same, but the x and y are
  208. replaced with these new values.
  209. */
  210. MouseEvent withNewPosition (Point<int> newPosition) const noexcept;
  211. //==============================================================================
  212. /** Changes the application-wide setting for the double-click time limit.
  213. This is the maximum length of time between mouse-clicks for it to be
  214. considered a double-click. It's used by the Component class.
  215. @see getDoubleClickTimeout, MouseListener::mouseDoubleClick
  216. */
  217. static void setDoubleClickTimeout (int timeOutMilliseconds) noexcept;
  218. /** Returns the application-wide setting for the double-click time limit.
  219. This is the maximum length of time between mouse-clicks for it to be
  220. considered a double-click. It's used by the Component class.
  221. @see setDoubleClickTimeout, MouseListener::mouseDoubleClick
  222. */
  223. static int getDoubleClickTimeout() noexcept;
  224. private:
  225. //==============================================================================
  226. const Point<int> mouseDownPos;
  227. const uint8 numberOfClicks, wasMovedSinceMouseDown;
  228. MouseEvent& operator= (const MouseEvent&);
  229. };
  230. //==============================================================================
  231. /**
  232. Contains status information about a mouse wheel event.
  233. @see MouseListener, MouseEvent
  234. */
  235. struct MouseWheelDetails
  236. {
  237. //==============================================================================
  238. /** The amount that the wheel has been moved in the X axis.
  239. If isReversed is true, then a negative deltaX means that the wheel has been
  240. pushed physically to the left.
  241. If isReversed is false, then a negative deltaX means that the wheel has been
  242. pushed physically to the right.
  243. */
  244. float deltaX;
  245. /** The amount that the wheel has been moved in the Y axis.
  246. If isReversed is true, then a negative deltaY means that the wheel has been
  247. pushed physically upwards.
  248. If isReversed is false, then a negative deltaY means that the wheel has been
  249. pushed physically downwards.
  250. */
  251. float deltaY;
  252. /** Indicates whether the user has reversed the direction of the wheel.
  253. See deltaX and deltaY for an explanation of the effects of this value.
  254. */
  255. bool isReversed;
  256. /** If true, then the wheel has continuous, un-stepped motion. */
  257. bool isSmooth;
  258. };
  259. #endif // __JUCE_MOUSEEVENT_JUCEHEADER__