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.

878 lines
41KB

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