The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

835 lines
38KB

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