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.

999 lines
46KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. A slider control for changing a value.
  24. The slider can be horizontal, vertical, or rotary, and can optionally have
  25. a text-box inside it to show an editable display of the current value.
  26. To use it, create a Slider object and use the setSliderStyle() method
  27. to set up the type you want. To set up the text-entry box, use setTextBoxStyle().
  28. To define the values that it can be set to, see the setRange() and setValue() methods.
  29. There are also lots of custom tweaks you can do by subclassing and overriding
  30. some of the virtual methods, such as changing the scaling, changing the format of
  31. the text display, custom ways of limiting the values, etc.
  32. You can register Slider::Listener objects with a slider, and they'll be called when
  33. the value changes.
  34. @see Slider::Listener
  35. */
  36. class JUCE_API Slider : public Component,
  37. public SettableTooltipClient
  38. {
  39. public:
  40. //==============================================================================
  41. /** The types of slider available.
  42. @see setSliderStyle, setRotaryParameters
  43. */
  44. enum SliderStyle
  45. {
  46. LinearHorizontal, /**< A traditional horizontal slider. */
  47. LinearVertical, /**< A traditional vertical slider. */
  48. LinearBar, /**< A horizontal bar slider with the text label drawn on top of it. */
  49. LinearBarVertical, /**< A vertical bar slider with the text label drawn on top of it. */
  50. Rotary, /**< A rotary control that you move by dragging the mouse in a circular motion, like a knob.
  51. @see setRotaryParameters */
  52. RotaryHorizontalDrag, /**< A rotary control that you move by dragging the mouse left-to-right.
  53. @see setRotaryParameters */
  54. RotaryVerticalDrag, /**< A rotary control that you move by dragging the mouse up-and-down.
  55. @see setRotaryParameters */
  56. RotaryHorizontalVerticalDrag, /**< A rotary control that you move by dragging the mouse up-and-down or left-to-right.
  57. @see setRotaryParameters */
  58. IncDecButtons, /**< A pair of buttons that increment or decrement the slider's value by the increment set in setRange(). */
  59. TwoValueHorizontal, /**< A horizontal slider that has two thumbs instead of one, so it can show a minimum and maximum value.
  60. @see setMinValue, setMaxValue */
  61. TwoValueVertical, /**< A vertical slider that has two thumbs instead of one, so it can show a minimum and maximum value.
  62. @see setMinValue, setMaxValue */
  63. ThreeValueHorizontal, /**< A horizontal slider that has three thumbs instead of one, so it can show a minimum and maximum
  64. value, with the current value being somewhere between them.
  65. @see setMinValue, setMaxValue */
  66. ThreeValueVertical /**< A vertical slider that has three thumbs instead of one, so it can show a minimum and maximum
  67. value, with the current value being somewhere between them.
  68. @see setMinValue, setMaxValue */
  69. };
  70. /** The position of the slider's text-entry box.
  71. @see setTextBoxStyle
  72. */
  73. enum TextEntryBoxPosition
  74. {
  75. NoTextBox, /**< Doesn't display a text box. */
  76. TextBoxLeft, /**< Puts the text box to the left of the slider, vertically centred. */
  77. TextBoxRight, /**< Puts the text box to the right of the slider, vertically centred. */
  78. TextBoxAbove, /**< Puts the text box above the slider, horizontally centred. */
  79. TextBoxBelow /**< Puts the text box below the slider, horizontally centred. */
  80. };
  81. /** Describes the type of mouse-dragging that is happening when a value is being changed.
  82. @see snapValue
  83. */
  84. enum DragMode
  85. {
  86. notDragging, /**< Dragging is not active. */
  87. absoluteDrag, /**< The dragging corresponds directly to the value that is displayed. */
  88. velocityDrag /**< The dragging value change is relative to the velocity of the mouse mouvement. */
  89. };
  90. //==============================================================================
  91. /** Creates a slider.
  92. When created, you can set up the slider's style and range with setSliderStyle(), setRange(), etc.
  93. */
  94. Slider();
  95. /** Creates a slider.
  96. When created, you can set up the slider's style and range with setSliderStyle(), setRange(), etc.
  97. */
  98. explicit Slider (const String& componentName);
  99. /** Creates a slider with some explicit options. */
  100. Slider (SliderStyle style, TextEntryBoxPosition textBoxPosition);
  101. /** Destructor. */
  102. ~Slider();
  103. //==============================================================================
  104. /** Changes the type of slider interface being used.
  105. @param newStyle the type of interface
  106. @see setRotaryParameters, setVelocityBasedMode
  107. */
  108. void setSliderStyle (SliderStyle newStyle);
  109. /** Returns the slider's current style.
  110. @see setSliderStyle
  111. */
  112. SliderStyle getSliderStyle() const noexcept;
  113. //==============================================================================
  114. struct RotaryParameters
  115. {
  116. /** The angle (in radians, clockwise from the top) at which
  117. the slider's minimum value is represented.
  118. */
  119. float startAngleRadians;
  120. /** The angle (in radians, clockwise from the top) at which
  121. the slider's maximum value is represented. This must be
  122. greater than startAngleRadians.
  123. */
  124. float endAngleRadians;
  125. /** Determines what happens when a circular drag action rotates beyond
  126. the minimum or maximum angle. If true, the value will stop changing
  127. until the mouse moves back the way it came; if false, the value
  128. will snap back to the value nearest to the mouse. Note that this has
  129. no effect if the drag mode is vertical or horizontal.
  130. */
  131. bool stopAtEnd;
  132. };
  133. /** Changes the properties of a rotary slider. */
  134. void setRotaryParameters (RotaryParameters newParameters) noexcept;
  135. /** Changes the properties of a rotary slider. */
  136. void setRotaryParameters (float startAngleRadians,
  137. float endAngleRadians,
  138. bool stopAtEnd) noexcept;
  139. /** Changes the properties of a rotary slider. */
  140. RotaryParameters getRotaryParameters() const noexcept;
  141. /** Sets the distance the mouse has to move to drag the slider across
  142. the full extent of its range.
  143. This only applies when in modes like RotaryHorizontalDrag, where it's using
  144. relative mouse movements to adjust the slider.
  145. */
  146. void setMouseDragSensitivity (int distanceForFullScaleDrag);
  147. /** Returns the current sensitivity value set by setMouseDragSensitivity(). */
  148. int getMouseDragSensitivity() const noexcept;
  149. //==============================================================================
  150. /** Changes the way the mouse is used when dragging the slider.
  151. If true, this will turn on velocity-sensitive dragging, so that
  152. the faster the mouse moves, the bigger the movement to the slider. This
  153. helps when making accurate adjustments if the slider's range is quite large.
  154. If false, the slider will just try to snap to wherever the mouse is.
  155. */
  156. void setVelocityBasedMode (bool isVelocityBased);
  157. /** Returns true if velocity-based mode is active.
  158. @see setVelocityBasedMode
  159. */
  160. bool getVelocityBasedMode() const noexcept;
  161. /** Changes aspects of the scaling used when in velocity-sensitive mode.
  162. These apply when you've used setVelocityBasedMode() to turn on velocity mode,
  163. or if you're holding down ctrl.
  164. @param sensitivity higher values than 1.0 increase the range of acceleration used
  165. @param threshold the minimum number of pixels that the mouse needs to move for it
  166. to be treated as a movement
  167. @param offset values greater than 0.0 increase the minimum speed that will be used when
  168. the threshold is reached
  169. @param userCanPressKeyToSwapMode if true, then the user can hold down the ctrl or command
  170. key to toggle velocity-sensitive mode
  171. @param modifiersToSwapModes this is a set of modifier flags which will be tested when determining
  172. whether to enable/disable velocity-sensitive mode
  173. */
  174. void setVelocityModeParameters (double sensitivity = 1.0,
  175. int threshold = 1,
  176. double offset = 0.0,
  177. bool userCanPressKeyToSwapMode = true,
  178. ModifierKeys::Flags modifiersToSwapModes = ModifierKeys::ctrlAltCommandModifiers);
  179. /** Returns the velocity sensitivity setting.
  180. @see setVelocityModeParameters
  181. */
  182. double getVelocitySensitivity() const noexcept;
  183. /** Returns the velocity threshold setting.
  184. @see setVelocityModeParameters
  185. */
  186. int getVelocityThreshold() const noexcept;
  187. /** Returns the velocity offset setting.
  188. @see setVelocityModeParameters
  189. */
  190. double getVelocityOffset() const noexcept;
  191. /** Returns the velocity user key setting.
  192. @see setVelocityModeParameters
  193. */
  194. bool getVelocityModeIsSwappable() const noexcept;
  195. //==============================================================================
  196. /** Sets up a skew factor to alter the way values are distributed.
  197. You may want to use a range of values on the slider where more accuracy
  198. is required towards one end of the range, so this will logarithmically
  199. spread the values across the length of the slider.
  200. If the factor is < 1.0, the lower end of the range will fill more of the
  201. slider's length; if the factor is > 1.0, the upper end of the range
  202. will be expanded instead. A factor of 1.0 doesn't skew it at all.
  203. If symmetricSkew is true, the skew factor applies from the middle of the slider
  204. to each of its ends.
  205. To set the skew position by using a mid-point, use the setSkewFactorFromMidPoint()
  206. method instead.
  207. @see getSkewFactor, setSkewFactorFromMidPoint, isSymmetricSkew
  208. */
  209. void setSkewFactor (double factor, bool symmetricSkew = false);
  210. /** Sets up a skew factor to alter the way values are distributed.
  211. This allows you to specify the slider value that should appear in the
  212. centre of the slider's visible range.
  213. @see setSkewFactor, getSkewFactor, isSymmetricSkew
  214. */
  215. void setSkewFactorFromMidPoint (double sliderValueToShowAtMidPoint);
  216. /** Returns the current skew factor.
  217. See setSkewFactor for more info.
  218. @see setSkewFactor, setSkewFactorFromMidPoint, isSymmetricSkew
  219. */
  220. double getSkewFactor() const noexcept;
  221. /** Returns the whether the skew is symmetric from the midpoint to both sides.
  222. See setSkewFactor for more info.
  223. @see getSkewFactor, setSkewFactor, setSkewFactorFromMidPoint
  224. */
  225. bool isSymmetricSkew() const noexcept;
  226. //==============================================================================
  227. /** Used by setIncDecButtonsMode().
  228. */
  229. enum IncDecButtonMode
  230. {
  231. incDecButtonsNotDraggable,
  232. incDecButtonsDraggable_AutoDirection,
  233. incDecButtonsDraggable_Horizontal,
  234. incDecButtonsDraggable_Vertical
  235. };
  236. /** When the style is IncDecButtons, this lets you turn on a mode where the mouse
  237. can be dragged on the buttons to drag the values.
  238. By default this is turned off. When enabled, clicking on the buttons still works
  239. them as normal, but by holding down the mouse on a button and dragging it a little
  240. distance, it flips into a mode where the value can be dragged. The drag direction can
  241. either be set explicitly to be vertical or horizontal, or can be set to
  242. incDecButtonsDraggable_AutoDirection so that it depends on whether the buttons
  243. are side-by-side or above each other.
  244. */
  245. void setIncDecButtonsMode (IncDecButtonMode mode);
  246. //==============================================================================
  247. /** Changes the location and properties of the text-entry box.
  248. @param newPosition where it should go (or NoTextBox to not have one at all)
  249. @param isReadOnly if true, it's a read-only display
  250. @param textEntryBoxWidth the width of the text-box in pixels. Make sure this leaves enough
  251. room for the slider as well!
  252. @param textEntryBoxHeight the height of the text-box in pixels. Make sure this leaves enough
  253. room for the slider as well!
  254. @see setTextBoxIsEditable, getValueFromText, getTextFromValue
  255. */
  256. void setTextBoxStyle (TextEntryBoxPosition newPosition,
  257. bool isReadOnly,
  258. int textEntryBoxWidth,
  259. int textEntryBoxHeight);
  260. /** Returns the status of the text-box.
  261. @see setTextBoxStyle
  262. */
  263. TextEntryBoxPosition getTextBoxPosition() const noexcept;
  264. /** Returns the width used for the text-box.
  265. @see setTextBoxStyle
  266. */
  267. int getTextBoxWidth() const noexcept;
  268. /** Returns the height used for the text-box.
  269. @see setTextBoxStyle
  270. */
  271. int getTextBoxHeight() const noexcept;
  272. /** Makes the text-box editable.
  273. By default this is true, and the user can enter values into the textbox,
  274. but it can be turned off if that's not suitable.
  275. @see setTextBoxStyle, getValueFromText, getTextFromValue
  276. */
  277. void setTextBoxIsEditable (bool shouldBeEditable);
  278. /** Returns true if the text-box is read-only.
  279. @see setTextBoxStyle
  280. */
  281. bool isTextBoxEditable() const noexcept;
  282. /** If the text-box is editable, this will give it the focus so that the user can
  283. type directly into it.
  284. This is basically the effect as the user clicking on it.
  285. */
  286. void showTextBox();
  287. /** If the text-box currently has focus and is being edited, this resets it and takes keyboard
  288. focus away from it.
  289. @param discardCurrentEditorContents if true, the slider's value will be left
  290. unchanged; if false, the current contents of the
  291. text editor will be used to set the slider position
  292. before it is hidden.
  293. */
  294. void hideTextBox (bool discardCurrentEditorContents);
  295. //==============================================================================
  296. /** Changes the slider's current value.
  297. This will trigger a callback to Slider::Listener::sliderValueChanged() for any listeners
  298. that are registered, and will synchronously call the valueChanged() method in case subclasses
  299. want to handle it.
  300. @param newValue the new value to set - this will be restricted by the
  301. minimum and maximum range, and will be snapped to the
  302. nearest interval if one has been set
  303. @param notification can be one of the NotificationType values, to request
  304. a synchronous or asynchronous call to the valueChanged() method
  305. of any Slider::Listeners that are registered. A notification will
  306. only be sent if the Slider's value has changed.
  307. */
  308. void setValue (double newValue, NotificationType notification = sendNotificationAsync);
  309. /** Returns the slider's current value. */
  310. double getValue() const;
  311. /** Returns the Value object that represents the slider's current position.
  312. You can use this Value object to connect the slider's position to external values or setters,
  313. either by taking a copy of the Value, or by using Value::referTo() to make it point to
  314. your own Value object.
  315. @see Value, getMaxValue, getMinValueObject
  316. */
  317. Value& getValueObject() noexcept;
  318. //==============================================================================
  319. /** Sets the limits that the slider's value can take.
  320. @param newMinimum the lowest value allowed
  321. @param newMaximum the highest value allowed
  322. @param newInterval the steps in which the value is allowed to increase - if this
  323. is not zero, the value will always be (newMinimum + (newInterval * an integer)).
  324. */
  325. void setRange (double newMinimum,
  326. double newMaximum,
  327. double newInterval = 0);
  328. /** Sets the limits that the slider's value can take.
  329. @param newRange the range to allow
  330. @param newInterval the steps in which the value is allowed to increase - if this
  331. is not zero, the value will always be (newMinimum + (newInterval * an integer)).
  332. */
  333. void setRange (Range<double> newRange, double newInterval);
  334. /** Sets a NormalisableRange to use for the Slider values.
  335. @param newNormalisableRange the NormalisableRange to use
  336. */
  337. void setNormalisableRange (NormalisableRange<double> newNormalisableRange);
  338. /** Returns the slider's range. */
  339. Range<double> getRange() const noexcept;
  340. /** Returns the current maximum value.
  341. @see setRange, getRange
  342. */
  343. double getMaximum() const noexcept;
  344. /** Returns the current minimum value.
  345. @see setRange, getRange
  346. */
  347. double getMinimum() const noexcept;
  348. /** Returns the current step-size for values.
  349. @see setRange, getRange
  350. */
  351. double getInterval() const noexcept;
  352. //==============================================================================
  353. /** For a slider with two or three thumbs, this returns the lower of its values.
  354. For a two-value slider, the values are controlled with getMinValue() and getMaxValue().
  355. A slider with three values also uses the normal getValue() and setValue() methods to
  356. control the middle value.
  357. @see setMinValue, getMaxValue, TwoValueHorizontal, TwoValueVertical, ThreeValueHorizontal, ThreeValueVertical
  358. */
  359. double getMinValue() const;
  360. /** For a slider with two or three thumbs, this returns the lower of its values.
  361. You can use this Value object to connect the slider's position to external values or setters,
  362. either by taking a copy of the Value, or by using Value::referTo() to make it point to
  363. your own Value object.
  364. @see Value, getMinValue, getMaxValueObject
  365. */
  366. Value& getMinValueObject() noexcept;
  367. /** For a slider with two or three thumbs, this sets the lower of its values.
  368. This will trigger a callback to Slider::Listener::sliderValueChanged() for any listeners
  369. that are registered, and will synchronously call the valueChanged() method in case subclasses
  370. want to handle it.
  371. @param newValue the new value to set - this will be restricted by the
  372. minimum and maximum range, and will be snapped to the nearest
  373. interval if one has been set.
  374. @param notification can be one of the NotificationType values, to request
  375. a synchronous or asynchronous call to the valueChanged() method
  376. of any Slider::Listeners that are registered. A notification will
  377. only be sent if this value has changed.
  378. @param allowNudgingOfOtherValues if false, this value will be restricted to being below the
  379. max value (in a two-value slider) or the mid value (in a three-value
  380. slider). If true, then if this value goes beyond those values,
  381. it will push them along with it.
  382. @see getMinValue, setMaxValue, setValue
  383. */
  384. void setMinValue (double newValue,
  385. NotificationType notification = sendNotificationAsync,
  386. bool allowNudgingOfOtherValues = false);
  387. /** For a slider with two or three thumbs, this returns the higher of its values.
  388. For a two-value slider, the values are controlled with getMinValue() and getMaxValue().
  389. A slider with three values also uses the normal getValue() and setValue() methods to
  390. control the middle value.
  391. @see getMinValue, TwoValueHorizontal, TwoValueVertical, ThreeValueHorizontal, ThreeValueVertical
  392. */
  393. double getMaxValue() const;
  394. /** For a slider with two or three thumbs, this returns the higher of its values.
  395. You can use this Value object to connect the slider's position to external values or setters,
  396. either by taking a copy of the Value, or by using Value::referTo() to make it point to
  397. your own Value object.
  398. @see Value, getMaxValue, getMinValueObject
  399. */
  400. Value& getMaxValueObject() noexcept;
  401. /** For a slider with two or three thumbs, this sets the lower of its values.
  402. This will trigger a callback to Slider::Listener::sliderValueChanged() for any listeners
  403. that are registered, and will synchronously call the valueChanged() method in case subclasses
  404. want to handle it.
  405. @param newValue the new value to set - this will be restricted by the
  406. minimum and maximum range, and will be snapped to the nearest
  407. interval if one has been set.
  408. @param notification can be one of the NotificationType values, to request
  409. a synchronous or asynchronous call to the valueChanged() method
  410. of any Slider::Listeners that are registered. A notification will
  411. only be sent if this value has changed.
  412. @param allowNudgingOfOtherValues if false, this value will be restricted to being above the
  413. min value (in a two-value slider) or the mid value (in a three-value
  414. slider). If true, then if this value goes beyond those values,
  415. it will push them along with it.
  416. @see getMaxValue, setMinValue, setValue
  417. */
  418. void setMaxValue (double newValue,
  419. NotificationType notification = sendNotificationAsync,
  420. bool allowNudgingOfOtherValues = false);
  421. /** For a slider with two or three thumbs, this sets the minimum and maximum thumb positions.
  422. This will trigger a callback to Slider::Listener::sliderValueChanged() for any listeners
  423. that are registered, and will synchronously call the valueChanged() method in case subclasses
  424. want to handle it.
  425. @param newMinValue the new minimum value to set - this will be snapped to the
  426. nearest interval if one has been set.
  427. @param newMaxValue the new minimum value to set - this will be snapped to the
  428. nearest interval if one has been set.
  429. @param notification can be one of the NotificationType values, to request
  430. a synchronous or asynchronous call to the valueChanged() method
  431. of any Slider::Listeners that are registered. A notification will
  432. only be sent if one or more of the values has changed.
  433. @see setMaxValue, setMinValue, setValue
  434. */
  435. void setMinAndMaxValues (double newMinValue, double newMaxValue,
  436. NotificationType notification = sendNotificationAsync);
  437. //==============================================================================
  438. /** A class for receiving callbacks from a Slider.
  439. To be told when a slider's value changes, you can register a Slider::Listener
  440. object using Slider::addListener().
  441. @see Slider::addListener, Slider::removeListener
  442. */
  443. class JUCE_API Listener
  444. {
  445. public:
  446. //==============================================================================
  447. /** Destructor. */
  448. virtual ~Listener() {}
  449. //==============================================================================
  450. /** Called when the slider's value is changed.
  451. This may be caused by dragging it, or by typing in its text entry box,
  452. or by a call to Slider::setValue().
  453. You can find out the new value using Slider::getValue().
  454. @see Slider::valueChanged
  455. */
  456. virtual void sliderValueChanged (Slider* slider) = 0;
  457. //==============================================================================
  458. /** Called when the slider is about to be dragged.
  459. This is called when a drag begins, then it's followed by multiple calls
  460. to sliderValueChanged(), and then sliderDragEnded() is called after the
  461. user lets go.
  462. @see sliderDragEnded, Slider::startedDragging
  463. */
  464. virtual void sliderDragStarted (Slider*) {}
  465. /** Called after a drag operation has finished.
  466. @see sliderDragStarted, Slider::stoppedDragging
  467. */
  468. virtual void sliderDragEnded (Slider*) {}
  469. };
  470. /** Adds a listener to be called when this slider's value changes. */
  471. void addListener (Listener* listener);
  472. /** Removes a previously-registered listener. */
  473. void removeListener (Listener* listener);
  474. //==============================================================================
  475. /** You can assign a lambda to this callback object to have it called when the slider value is changed. */
  476. std::function<void()> onValueChange;
  477. /** You can assign a lambda to this callback object to have it called when the slider's drag begins. */
  478. std::function<void()> onDragStart;
  479. /** You can assign a lambda to this callback object to have it called when the slider's drag ends. */
  480. std::function<void()> onDragEnd;
  481. /** You can assign a lambda that will be used to convert textual values to the slider's normalised position. */
  482. std::function<double (const String&)> valueFromTextFunction;
  483. /** You can assign a lambda that will be used to convert the slider's normalised position to a textual value. */
  484. std::function<String (double)> textFromValueFunction;
  485. //==============================================================================
  486. /** This lets you choose whether double-clicking moves the slider to a given position.
  487. By default this is turned off, but it's handy if you want a double-click to act
  488. as a quick way of resetting a slider. Just pass in the value you want it to
  489. go to when double-clicked.
  490. @see getDoubleClickReturnValue
  491. */
  492. void setDoubleClickReturnValue (bool shouldDoubleClickBeEnabled,
  493. double valueToSetOnDoubleClick);
  494. /** Returns the values last set by setDoubleClickReturnValue() method.
  495. @see setDoubleClickReturnValue
  496. */
  497. double getDoubleClickReturnValue() const noexcept;
  498. /** Returns true if double-clicking to reset to a default value is enabled.
  499. @see setDoubleClickReturnValue
  500. */
  501. bool isDoubleClickReturnEnabled() const noexcept;
  502. //==============================================================================
  503. /** Tells the slider whether to keep sending change messages while the user
  504. is dragging the slider.
  505. If set to true, a change message will only be sent when the user has
  506. dragged the slider and let go. If set to false (the default), then messages
  507. will be continuously sent as they drag it while the mouse button is still
  508. held down.
  509. */
  510. void setChangeNotificationOnlyOnRelease (bool onlyNotifyOnRelease);
  511. /** This lets you change whether the slider thumb jumps to the mouse position
  512. when you click.
  513. By default, this is true. If it's false, then the slider moves with relative
  514. motion when you drag it.
  515. This only applies to linear bars, and won't affect two- or three- value
  516. sliders.
  517. */
  518. void setSliderSnapsToMousePosition (bool shouldSnapToMouse);
  519. /** Returns true if setSliderSnapsToMousePosition() has been enabled. */
  520. bool getSliderSnapsToMousePosition() const noexcept;
  521. /** If enabled, this gives the slider a pop-up bubble which appears while the
  522. slider is being dragged or hovered-over.
  523. This can be handy if your slider doesn't have a text-box, so that users can
  524. see the value just when they're changing it.
  525. If you pass a component as the parentComponentToUse parameter, the pop-up
  526. bubble will be added as a child of that component when it's needed. If you
  527. pass nullptr, the pop-up will be placed on the desktop instead (note that it's a
  528. transparent window, so if you're using an OS that can't do transparent windows
  529. you'll have to add it to a parent component instead).
  530. By default the popup display shown when hovering will remain visible for 2 seconds,
  531. but it is possible to change this by passing a different hoverTimeout value. A
  532. value of -1 will cause the popup to remain until a mouseExit() occurs on the slider.
  533. */
  534. void setPopupDisplayEnabled (bool shouldShowOnMouseDrag,
  535. bool shouldShowOnMouseHover,
  536. Component* parentComponentToUse,
  537. int hoverTimeout = 2000);
  538. /** If a popup display is enabled and is currently visible, this returns the component
  539. that is being shown, or nullptr if none is currently in use.
  540. @see setPopupDisplayEnabled
  541. */
  542. Component* getCurrentPopupDisplay() const noexcept;
  543. /** If this is set to true, then right-clicking on the slider will pop-up
  544. a menu to let the user change the way it works.
  545. By default this is turned off, but when turned on, the menu will include
  546. things like velocity sensitivity, and for rotary sliders, whether they
  547. use a linear or rotary mouse-drag to move them.
  548. */
  549. void setPopupMenuEnabled (bool menuEnabled);
  550. /** This can be used to stop the mouse scroll-wheel from moving the slider.
  551. By default it's enabled.
  552. */
  553. void setScrollWheelEnabled (bool enabled);
  554. /** Returns a number to indicate which thumb is currently being dragged by the mouse.
  555. This will return 0 for the main thumb, 1 for the minimum-value thumb, 2 for
  556. the maximum-value thumb, or -1 if none is currently down.
  557. */
  558. int getThumbBeingDragged() const noexcept;
  559. //==============================================================================
  560. /** Callback to indicate that the user is about to start dragging the slider.
  561. @see Slider::Listener::sliderDragStarted
  562. */
  563. virtual void startedDragging();
  564. /** Callback to indicate that the user has just stopped dragging the slider.
  565. @see Slider::Listener::sliderDragEnded
  566. */
  567. virtual void stoppedDragging();
  568. /** Callback to indicate that the user has just moved the slider.
  569. @see Slider::Listener::sliderValueChanged
  570. */
  571. virtual void valueChanged();
  572. //==============================================================================
  573. /** Subclasses can override this to convert a text string to a value.
  574. When the user enters something into the text-entry box, this method is
  575. called to convert it to a value.
  576. The default implementation just tries to convert it to a double.
  577. @see getTextFromValue
  578. */
  579. virtual double getValueFromText (const String& text);
  580. /** Turns the slider's current value into a text string.
  581. Subclasses can override this to customise the formatting of the text-entry box.
  582. The default implementation just turns the value into a string, using
  583. a number of decimal places based on the range interval. If a suffix string
  584. has been set using setTextValueSuffix(), this will be appended to the text.
  585. @see getValueFromText
  586. */
  587. virtual String getTextFromValue (double value);
  588. /** Sets a suffix to append to the end of the numeric value when it's displayed as
  589. a string.
  590. This is used by the default implementation of getTextFromValue(), and is just
  591. appended to the numeric value. For more advanced formatting, you can override
  592. getTextFromValue() and do something else.
  593. */
  594. void setTextValueSuffix (const String& suffix);
  595. /** Returns the suffix that was set by setTextValueSuffix(). */
  596. String getTextValueSuffix() const;
  597. /** Returns the best number of decimal places to use when displaying this
  598. slider's value.
  599. It calculates the fewest decimal places needed to represent numbers with
  600. the slider's interval setting.
  601. @see setNumDecimalPlacesToDisplay
  602. */
  603. int getNumDecimalPlacesToDisplay() const noexcept;
  604. /** Modifies the best number of decimal places to use when displaying this
  605. slider's value.
  606. @see getNumDecimalPlacesToDisplay
  607. */
  608. void setNumDecimalPlacesToDisplay (int decimalPlacesToDisplay);
  609. //==============================================================================
  610. /** Allows a user-defined mapping of distance along the slider to its value.
  611. The default implementation for this performs the skewing operation that
  612. can be set up in the setSkewFactor() method. Override it if you need
  613. some kind of custom mapping instead, but make sure you also implement the
  614. inverse function in valueToProportionOfLength().
  615. @param proportion a value 0 to 1.0, indicating a distance along the slider
  616. @returns the slider value that is represented by this position
  617. @see valueToProportionOfLength
  618. */
  619. virtual double proportionOfLengthToValue (double proportion);
  620. /** Allows a user-defined mapping of value to the position of the slider along its length.
  621. The default implementation for this performs the skewing operation that
  622. can be set up in the setSkewFactor() method. Override it if you need
  623. some kind of custom mapping instead, but make sure you also implement the
  624. inverse function in proportionOfLengthToValue().
  625. @param value a valid slider value, between the range of values specified in
  626. setRange()
  627. @returns a value 0 to 1.0 indicating the distance along the slider that
  628. represents this value
  629. @see proportionOfLengthToValue
  630. */
  631. virtual double valueToProportionOfLength (double value);
  632. /** Returns the X or Y coordinate of a value along the slider's length.
  633. If the slider is horizontal, this will be the X coordinate of the given
  634. value, relative to the left of the slider. If it's vertical, then this will
  635. be the Y coordinate, relative to the top of the slider.
  636. If the slider is rotary, this will throw an assertion and return 0. If the
  637. value is out-of-range, it will be constrained to the length of the slider.
  638. */
  639. float getPositionOfValue (double value) const;
  640. //==============================================================================
  641. /** This can be overridden to allow the slider to snap to user-definable values.
  642. If overridden, it will be called when the user tries to move the slider to
  643. a given position, and allows a subclass to sanity-check this value, possibly
  644. returning a different value to use instead.
  645. @param attemptedValue the value the user is trying to enter
  646. @param dragMode indicates whether the user is dragging with
  647. the mouse; notDragging if they are entering the value
  648. using the text box or other non-dragging interaction
  649. @returns the value to use instead
  650. */
  651. virtual double snapValue (double attemptedValue, DragMode dragMode);
  652. //==============================================================================
  653. /** This can be called to force the text box to update its contents.
  654. (Not normally needed, as this is done automatically).
  655. */
  656. void updateText();
  657. /** True if the slider moves horizontally. */
  658. bool isHorizontal() const noexcept;
  659. /** True if the slider moves vertically. */
  660. bool isVertical() const noexcept;
  661. /** True if the slider is in a rotary mode. */
  662. bool isRotary() const noexcept;
  663. /** True if the slider is in a linear bar mode. */
  664. bool isBar() const noexcept;
  665. //==============================================================================
  666. /** A set of colour IDs to use to change the colour of various aspects of the slider.
  667. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  668. methods.
  669. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  670. */
  671. enum ColourIds
  672. {
  673. backgroundColourId = 0x1001200, /**< A colour to use to fill the slider's background. */
  674. thumbColourId = 0x1001300, /**< The colour to draw the thumb with. It's up to the look
  675. and feel class how this is used. */
  676. trackColourId = 0x1001310, /**< The colour to draw the groove that the thumb moves along. */
  677. rotarySliderFillColourId = 0x1001311, /**< For rotary sliders, this colour fills the outer curve. */
  678. rotarySliderOutlineColourId = 0x1001312, /**< For rotary sliders, this colour is used to draw the outer curve's outline. */
  679. textBoxTextColourId = 0x1001400, /**< The colour for the text in the text-editor box used for editing the value. */
  680. textBoxBackgroundColourId = 0x1001500, /**< The background colour for the text-editor box. */
  681. textBoxHighlightColourId = 0x1001600, /**< The text highlight colour for the text-editor box. */
  682. textBoxOutlineColourId = 0x1001700 /**< The colour to use for a border around the text-editor box. */
  683. };
  684. //==============================================================================
  685. /** A struct defining the placement of the slider area and the text box area
  686. relative to the bounds of the whole Slider component.
  687. */
  688. struct SliderLayout
  689. {
  690. Rectangle<int> sliderBounds;
  691. Rectangle<int> textBoxBounds;
  692. };
  693. //==============================================================================
  694. /** This abstract base class is implemented by LookAndFeel classes to provide
  695. slider drawing functionality.
  696. */
  697. struct JUCE_API LookAndFeelMethods
  698. {
  699. virtual ~LookAndFeelMethods() {}
  700. //==============================================================================
  701. virtual void drawLinearSlider (Graphics&,
  702. int x, int y, int width, int height,
  703. float sliderPos,
  704. float minSliderPos,
  705. float maxSliderPos,
  706. const Slider::SliderStyle,
  707. Slider&) = 0;
  708. virtual void drawLinearSliderBackground (Graphics&,
  709. int x, int y, int width, int height,
  710. float sliderPos,
  711. float minSliderPos,
  712. float maxSliderPos,
  713. const Slider::SliderStyle style,
  714. Slider&) = 0;
  715. virtual void drawLinearSliderThumb (Graphics&,
  716. int x, int y, int width, int height,
  717. float sliderPos,
  718. float minSliderPos,
  719. float maxSliderPos,
  720. const Slider::SliderStyle,
  721. Slider&) = 0;
  722. virtual int getSliderThumbRadius (Slider&) = 0;
  723. virtual void drawRotarySlider (Graphics&,
  724. int x, int y, int width, int height,
  725. float sliderPosProportional,
  726. float rotaryStartAngle,
  727. float rotaryEndAngle,
  728. Slider&) = 0;
  729. virtual Button* createSliderButton (Slider&, bool isIncrement) = 0;
  730. virtual Label* createSliderTextBox (Slider&) = 0;
  731. virtual ImageEffectFilter* getSliderEffect (Slider&) = 0;
  732. virtual Font getSliderPopupFont (Slider&) = 0;
  733. virtual int getSliderPopupPlacement (Slider&) = 0;
  734. virtual SliderLayout getSliderLayout (Slider&) = 0;
  735. #if JUCE_CATCH_DEPRECATED_CODE_MISUSE
  736. // These methods' parameters have changed: see the new method signatures.
  737. virtual void createSliderButton (bool) {}
  738. virtual void getSliderEffect() {}
  739. virtual void getSliderPopupFont() {}
  740. virtual void getSliderPopupPlacement() {}
  741. #endif
  742. };
  743. //==============================================================================
  744. /** @internal */
  745. void paint (Graphics&) override;
  746. /** @internal */
  747. void resized() override;
  748. /** @internal */
  749. void mouseDown (const MouseEvent&) override;
  750. /** @internal */
  751. void mouseUp (const MouseEvent&) override;
  752. /** @internal */
  753. void mouseDrag (const MouseEvent&) override;
  754. /** @internal */
  755. void mouseDoubleClick (const MouseEvent&) override;
  756. /** @internal */
  757. void mouseWheelMove (const MouseEvent&, const MouseWheelDetails&) override;
  758. /** @internal */
  759. void modifierKeysChanged (const ModifierKeys&) override;
  760. /** @internal */
  761. void lookAndFeelChanged() override;
  762. /** @internal */
  763. void enablementChanged() override;
  764. /** @internal */
  765. void focusOfChildComponentChanged (FocusChangeType) override;
  766. /** @internal */
  767. void colourChanged() override;
  768. /** @internal */
  769. void mouseMove (const MouseEvent&) override;
  770. /** @internal */
  771. void mouseExit (const MouseEvent&) override;
  772. /** @internal */
  773. void mouseEnter (const MouseEvent&) override;
  774. private:
  775. //==============================================================================
  776. JUCE_PUBLIC_IN_DLL_BUILD (class Pimpl)
  777. friend class Pimpl;
  778. friend struct ContainerDeletePolicy<Pimpl>;
  779. ScopedPointer<Pimpl> pimpl;
  780. void init (SliderStyle, TextEntryBoxPosition);
  781. #if JUCE_CATCH_DEPRECATED_CODE_MISUSE
  782. // These methods' bool parameters have changed: see the new method signature.
  783. JUCE_DEPRECATED (void setValue (double, bool));
  784. JUCE_DEPRECATED (void setValue (double, bool, bool));
  785. JUCE_DEPRECATED (void setMinValue (double, bool, bool, bool));
  786. JUCE_DEPRECATED (void setMinValue (double, bool, bool));
  787. JUCE_DEPRECATED (void setMinValue (double, bool));
  788. JUCE_DEPRECATED (void setMaxValue (double, bool, bool, bool));
  789. JUCE_DEPRECATED (void setMaxValue (double, bool, bool));
  790. JUCE_DEPRECATED (void setMaxValue (double, bool));
  791. JUCE_DEPRECATED (void setMinAndMaxValues (double, double, bool, bool));
  792. JUCE_DEPRECATED (void setMinAndMaxValues (double, double, bool));
  793. virtual void snapValue (double, bool) {}
  794. #endif
  795. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Slider)
  796. };
  797. } // namespace juce