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.

1023 lines
47KB

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