Audio plugin host https://kx.studio/carla
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.

365 lines
15KB

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