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.

990 lines
46KB

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