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.

360 lines
14KB

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