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.

833 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. Rotary, /**< A rotary control that you move by dragging the mouse in a circular motion, like a knob.
  51. @see setRotaryParameters */
  52. RotaryHorizontalDrag, /**< A rotary control that you move by dragging the mouse left-to-right.
  53. @see setRotaryParameters */
  54. RotaryVerticalDrag, /**< A rotary control that you move by dragging the mouse up-and-down.
  55. @see setRotaryParameters */
  56. RotaryHorizontalVerticalDrag, /**< A rotary control that you move by dragging the mouse up-and-down or left-to-right.
  57. @see setRotaryParameters */
  58. IncDecButtons, /**< A pair of buttons that increment or decrement the slider's value by the increment set in setRange(). */
  59. TwoValueHorizontal, /**< A horizontal slider that has two thumbs instead of one, so it can show a minimum and maximum value.
  60. @see setMinValue, setMaxValue */
  61. TwoValueVertical, /**< A vertical slider that has two thumbs instead of one, so it can show a minimum and maximum value.
  62. @see setMinValue, setMaxValue */
  63. ThreeValueHorizontal, /**< A horizontal slider that has three thumbs instead of one, so it can show a minimum and maximum
  64. value, with the current value being somewhere between them.
  65. @see setMinValue, setMaxValue */
  66. ThreeValueVertical, /**< A vertical slider that has three thumbs instead of one, so it can show a minimum and maximum
  67. value, with the current value being somewhere between them.
  68. @see setMinValue, setMaxValue */
  69. };
  70. /** The position of the slider's text-entry box.
  71. @see setTextBoxStyle
  72. */
  73. enum TextEntryBoxPosition
  74. {
  75. NoTextBox, /**< Doesn't display a text box. */
  76. TextBoxLeft, /**< Puts the text box to the left of the slider, vertically centred. */
  77. TextBoxRight, /**< Puts the text box to the right of the slider, vertically centred. */
  78. TextBoxAbove, /**< Puts the text box above the slider, horizontally centred. */
  79. TextBoxBelow /**< Puts the text box below the slider, horizontally centred. */
  80. };
  81. //==============================================================================
  82. /** Creates a slider.
  83. When created, you can set up the slider's style and range with setSliderStyle(), setRange(), etc.
  84. */
  85. Slider();
  86. /** Creates a slider.
  87. When created, you can set up the slider's style and range with setSliderStyle(), setRange(), etc.
  88. */
  89. explicit Slider (const String& componentName);
  90. /** Creates a slider with some explicit options.
  91. */
  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 sendUpdateMessage if false, a change to the value will not trigger a call to
  272. any Slider::Listeners or the valueChanged() method
  273. @param sendMessageSynchronously if true, then a call to the Slider::Listeners will be made
  274. synchronously; if false, it will be asynchronous
  275. */
  276. void setValue (double newValue,
  277. bool sendUpdateMessage = true,
  278. bool sendMessageSynchronously = false);
  279. /** Returns the slider's current value. */
  280. double getValue() const;
  281. /** Returns the Value object that represents the slider's current position.
  282. You can use this Value object to connect the slider's position to external values or setters,
  283. either by taking a copy of the Value, or by using Value::referTo() to make it point to
  284. your own Value object.
  285. @see Value, getMaxValue, getMinValueObject
  286. */
  287. Value& getValueObject() noexcept;
  288. //==============================================================================
  289. /** Sets the limits that the slider's value can take.
  290. @param newMinimum the lowest value allowed
  291. @param newMaximum the highest value allowed
  292. @param newInterval the steps in which the value is allowed to increase - if this
  293. is not zero, the value will always be (newMinimum + (newInterval * an integer)).
  294. */
  295. void setRange (double newMinimum,
  296. double newMaximum,
  297. double newInterval = 0);
  298. /** Returns the current maximum value.
  299. @see setRange
  300. */
  301. double getMaximum() const noexcept;
  302. /** Returns the current minimum value.
  303. @see setRange
  304. */
  305. double getMinimum() const noexcept;
  306. /** Returns the current step-size for values.
  307. @see setRange
  308. */
  309. double getInterval() const noexcept;
  310. //==============================================================================
  311. /** For a slider with two or three thumbs, this returns the lower of its values.
  312. For a two-value slider, the values are controlled with getMinValue() and getMaxValue().
  313. A slider with three values also uses the normal getValue() and setValue() methods to
  314. control the middle value.
  315. @see setMinValue, getMaxValue, TwoValueHorizontal, TwoValueVertical, ThreeValueHorizontal, ThreeValueVertical
  316. */
  317. double getMinValue() const;
  318. /** For a slider with two or three thumbs, this returns the lower of its values.
  319. You can use this Value object to connect the slider's position to external values or setters,
  320. either by taking a copy of the Value, or by using Value::referTo() to make it point to
  321. your own Value object.
  322. @see Value, getMinValue, getMaxValueObject
  323. */
  324. Value& getMinValueObject() noexcept;
  325. /** For a slider with two or three thumbs, this sets the lower of its values.
  326. This will trigger a callback to Slider::Listener::sliderValueChanged() for any listeners
  327. that are registered, and will synchronously call the valueChanged() method in case subclasses
  328. want to handle it.
  329. @param newValue the new value to set - this will be restricted by the
  330. minimum and maximum range, and will be snapped to the nearest
  331. interval if one has been set.
  332. @param sendUpdateMessage if false, a change to the value will not trigger a call to
  333. any Slider::Listeners or the valueChanged() method
  334. @param sendMessageSynchronously if true, then a call to the Slider::Listeners will be made
  335. synchronously; if false, it will be asynchronous
  336. @param allowNudgingOfOtherValues if false, this value will be restricted to being below the
  337. max value (in a two-value slider) or the mid value (in a three-value
  338. slider). If true, then if this value goes beyond those values,
  339. it will push them along with it.
  340. @see getMinValue, setMaxValue, setValue
  341. */
  342. void setMinValue (double newValue,
  343. bool sendUpdateMessage = true,
  344. bool sendMessageSynchronously = false,
  345. bool allowNudgingOfOtherValues = false);
  346. /** For a slider with two or three thumbs, this returns the higher of its values.
  347. For a two-value slider, the values are controlled with getMinValue() and getMaxValue().
  348. A slider with three values also uses the normal getValue() and setValue() methods to
  349. control the middle value.
  350. @see getMinValue, TwoValueHorizontal, TwoValueVertical, ThreeValueHorizontal, ThreeValueVertical
  351. */
  352. double getMaxValue() const;
  353. /** For a slider with two or three thumbs, this returns the higher of its values.
  354. You can use this Value object to connect the slider's position to external values or setters,
  355. either by taking a copy of the Value, or by using Value::referTo() to make it point to
  356. your own Value object.
  357. @see Value, getMaxValue, getMinValueObject
  358. */
  359. Value& getMaxValueObject() noexcept;
  360. /** For a slider with two or three thumbs, this sets the lower of its values.
  361. This will trigger a callback to Slider::Listener::sliderValueChanged() for any listeners
  362. that are registered, and will synchronously call the valueChanged() method in case subclasses
  363. want to handle it.
  364. @param newValue the new value to set - this will be restricted by the
  365. minimum and maximum range, and will be snapped to the nearest
  366. interval if one has been set.
  367. @param sendUpdateMessage if false, a change to the value will not trigger a call to
  368. any Slider::Listeners or the valueChanged() method
  369. @param sendMessageSynchronously if true, then a call to the Slider::Listeners will be made
  370. synchronously; if false, it will be asynchronous
  371. @param allowNudgingOfOtherValues if false, this value will be restricted to being above the
  372. min value (in a two-value slider) or the mid value (in a three-value
  373. slider). If true, then if this value goes beyond those values,
  374. it will push them along with it.
  375. @see getMaxValue, setMinValue, setValue
  376. */
  377. void setMaxValue (double newValue,
  378. bool sendUpdateMessage = true,
  379. bool sendMessageSynchronously = false,
  380. bool allowNudgingOfOtherValues = false);
  381. /** For a slider with two or three thumbs, this sets the minimum and maximum thumb positions.
  382. This will trigger a callback to Slider::Listener::sliderValueChanged() for any listeners
  383. that are registered, and will synchronously call the valueChanged() method in case subclasses
  384. want to handle it.
  385. @param newMinValue the new minimum value to set - this will be snapped to the
  386. nearest interval if one has been set.
  387. @param newMaxValue the new minimum value to set - this will be snapped to the
  388. nearest interval if one has been set.
  389. @param sendUpdateMessage if false, a change to the value will not trigger a call to
  390. any Slider::Listeners or the valueChanged() method
  391. @param sendMessageSynchronously if true, then a call to the Slider::Listeners will be made
  392. synchronously; if false, it will be asynchronous
  393. @see setMaxValue, setMinValue, setValue
  394. */
  395. void setMinAndMaxValues (double newMinValue, double newMaxValue,
  396. bool sendUpdateMessage = true,
  397. bool sendMessageSynchronously = false);
  398. //==============================================================================
  399. /** A class for receiving callbacks from a Slider.
  400. To be told when a slider's value changes, you can register a Slider::Listener
  401. object using Slider::addListener().
  402. @see Slider::addListener, Slider::removeListener
  403. */
  404. class JUCE_API Listener
  405. {
  406. public:
  407. //==============================================================================
  408. /** Destructor. */
  409. virtual ~Listener() {}
  410. //==============================================================================
  411. /** Called when the slider's value is changed.
  412. This may be caused by dragging it, or by typing in its text entry box,
  413. or by a call to Slider::setValue().
  414. You can find out the new value using Slider::getValue().
  415. @see Slider::valueChanged
  416. */
  417. virtual void sliderValueChanged (Slider* slider) = 0;
  418. //==============================================================================
  419. /** Called when the slider is about to be dragged.
  420. This is called when a drag begins, then it's followed by multiple calls
  421. to sliderValueChanged(), and then sliderDragEnded() is called after the
  422. user lets go.
  423. @see sliderDragEnded, Slider::startedDragging
  424. */
  425. virtual void sliderDragStarted (Slider* slider);
  426. /** Called after a drag operation has finished.
  427. @see sliderDragStarted, Slider::stoppedDragging
  428. */
  429. virtual void sliderDragEnded (Slider* slider);
  430. };
  431. /** Adds a listener to be called when this slider's value changes. */
  432. void addListener (Listener* listener);
  433. /** Removes a previously-registered listener. */
  434. void removeListener (Listener* listener);
  435. //==============================================================================
  436. /** This lets you choose whether double-clicking moves the slider to a given position.
  437. By default this is turned off, but it's handy if you want a double-click to act
  438. as a quick way of resetting a slider. Just pass in the value you want it to
  439. go to when double-clicked.
  440. @see getDoubleClickReturnValue
  441. */
  442. void setDoubleClickReturnValue (bool isDoubleClickEnabled,
  443. double valueToSetOnDoubleClick);
  444. /** Returns the values last set by setDoubleClickReturnValue() method.
  445. Sets isEnabled to true if double-click is enabled, and returns the value
  446. that was set.
  447. @see setDoubleClickReturnValue
  448. */
  449. double getDoubleClickReturnValue (bool& isEnabled) const;
  450. //==============================================================================
  451. /** Tells the slider whether to keep sending change messages while the user
  452. is dragging the slider.
  453. If set to true, a change message will only be sent when the user has
  454. dragged the slider and let go. If set to false (the default), then messages
  455. will be continuously sent as they drag it while the mouse button is still
  456. held down.
  457. */
  458. void setChangeNotificationOnlyOnRelease (bool onlyNotifyOnRelease);
  459. /** This lets you change whether the slider thumb jumps to the mouse position
  460. when you click.
  461. By default, this is true. If it's false, then the slider moves with relative
  462. motion when you drag it.
  463. This only applies to linear bars, and won't affect two- or three- value
  464. sliders.
  465. */
  466. void setSliderSnapsToMousePosition (bool shouldSnapToMouse);
  467. /** Returns true if setSliderSnapsToMousePosition() has been enabled. */
  468. bool getSliderSnapsToMousePosition() const noexcept;
  469. /** If enabled, this gives the slider a pop-up bubble which appears while the
  470. slider is being dragged.
  471. This can be handy if your slider doesn't have a text-box, so that users can
  472. see the value just when they're changing it.
  473. If you pass a component as the parentComponentToUse parameter, the pop-up
  474. bubble will be added as a child of that component when it's needed. If you
  475. pass 0, the pop-up will be placed on the desktop instead (note that it's a
  476. transparent window, so if you're using an OS that can't do transparent windows
  477. you'll have to add it to a parent component instead).
  478. */
  479. void setPopupDisplayEnabled (bool isEnabled,
  480. Component* parentComponentToUse);
  481. /** If a popup display is enabled and is currently visible, this returns the component
  482. that is being shown, or nullptr if none is currently in use.
  483. @see setPopupDisplayEnabled
  484. */
  485. Component* getCurrentPopupDisplay() const noexcept;
  486. /** If this is set to true, then right-clicking on the slider will pop-up
  487. a menu to let the user change the way it works.
  488. By default this is turned off, but when turned on, the menu will include
  489. things like velocity sensitivity, and for rotary sliders, whether they
  490. use a linear or rotary mouse-drag to move them.
  491. */
  492. void setPopupMenuEnabled (bool menuEnabled);
  493. /** This can be used to stop the mouse scroll-wheel from moving the slider.
  494. By default it's enabled.
  495. */
  496. void setScrollWheelEnabled (bool enabled);
  497. /** Returns a number to indicate which thumb is currently being dragged by the
  498. mouse.
  499. This will return 0 for the main thumb, 1 for the minimum-value thumb, 2 for
  500. the maximum-value thumb, or -1 if none is currently down.
  501. */
  502. int getThumbBeingDragged() const noexcept;
  503. //==============================================================================
  504. /** Callback to indicate that the user is about to start dragging the slider.
  505. @see Slider::Listener::sliderDragStarted
  506. */
  507. virtual void startedDragging();
  508. /** Callback to indicate that the user has just stopped dragging the slider.
  509. @see Slider::Listener::sliderDragEnded
  510. */
  511. virtual void stoppedDragging();
  512. /** Callback to indicate that the user has just moved the slider.
  513. @see Slider::Listener::sliderValueChanged
  514. */
  515. virtual void valueChanged();
  516. //==============================================================================
  517. /** Subclasses can override this to convert a text string to a value.
  518. When the user enters something into the text-entry box, this method is
  519. called to convert it to a value.
  520. The default routine just tries to convert it to a double.
  521. @see getTextFromValue
  522. */
  523. virtual double getValueFromText (const String& text);
  524. /** Turns the slider's current value into a text string.
  525. Subclasses can override this to customise the formatting of the text-entry box.
  526. The default implementation just turns the value into a string, using
  527. a number of decimal places based on the range interval. If a suffix string
  528. has been set using setTextValueSuffix(), this will be appended to the text.
  529. @see getValueFromText
  530. */
  531. virtual String getTextFromValue (double value);
  532. /** Sets a suffix to append to the end of the numeric value when it's displayed as
  533. a string.
  534. This is used by the default implementation of getTextFromValue(), and is just
  535. appended to the numeric value. For more advanced formatting, you can override
  536. getTextFromValue() and do something else.
  537. */
  538. void setTextValueSuffix (const String& suffix);
  539. /** Returns the suffix that was set by setTextValueSuffix(). */
  540. String getTextValueSuffix() const;
  541. //==============================================================================
  542. /** Allows a user-defined mapping of distance along the slider to its value.
  543. The default implementation for this performs the skewing operation that
  544. can be set up in the setSkewFactor() method. Override it if you need
  545. some kind of custom mapping instead, but make sure you also implement the
  546. inverse function in valueToProportionOfLength().
  547. @param proportion a value 0 to 1.0, indicating a distance along the slider
  548. @returns the slider value that is represented by this position
  549. @see valueToProportionOfLength
  550. */
  551. virtual double proportionOfLengthToValue (double proportion);
  552. /** Allows a user-defined mapping of value to the position of the slider along its length.
  553. The default implementation for this performs the skewing operation that
  554. can be set up in the setSkewFactor() method. Override it if you need
  555. some kind of custom mapping instead, but make sure you also implement the
  556. inverse function in proportionOfLengthToValue().
  557. @param value a valid slider value, between the range of values specified in
  558. setRange()
  559. @returns a value 0 to 1.0 indicating the distance along the slider that
  560. represents this value
  561. @see proportionOfLengthToValue
  562. */
  563. virtual double valueToProportionOfLength (double value);
  564. /** Returns the X or Y coordinate of a value along the slider's length.
  565. If the slider is horizontal, this will be the X coordinate of the given
  566. value, relative to the left of the slider. If it's vertical, then this will
  567. be the Y coordinate, relative to the top of the slider.
  568. If the slider is rotary, this will throw an assertion and return 0. If the
  569. value is out-of-range, it will be constrained to the length of the slider.
  570. */
  571. float getPositionOfValue (double value);
  572. //==============================================================================
  573. /** This can be overridden to allow the slider to snap to user-definable values.
  574. If overridden, it will be called when the user tries to move the slider to
  575. a given position, and allows a subclass to sanity-check this value, possibly
  576. returning a different value to use instead.
  577. @param attemptedValue the value the user is trying to enter
  578. @param userIsDragging true if the user is dragging with the mouse; false if
  579. they are entering the value using the text box
  580. @returns the value to use instead
  581. */
  582. virtual double snapValue (double attemptedValue, bool userIsDragging);
  583. //==============================================================================
  584. /** This can be called to force the text box to update its contents.
  585. (Not normally needed, as this is done automatically).
  586. */
  587. void updateText();
  588. /** True if the slider moves horizontally. */
  589. bool isHorizontal() const noexcept;
  590. /** True if the slider moves vertically. */
  591. bool isVertical() const noexcept;
  592. //==============================================================================
  593. /** A set of colour IDs to use to change the colour of various aspects of the slider.
  594. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  595. methods.
  596. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  597. */
  598. enum ColourIds
  599. {
  600. backgroundColourId = 0x1001200, /**< A colour to use to fill the slider's background. */
  601. thumbColourId = 0x1001300, /**< The colour to draw the thumb with. It's up to the look
  602. and feel class how this is used. */
  603. trackColourId = 0x1001310, /**< The colour to draw the groove that the thumb moves along. */
  604. rotarySliderFillColourId = 0x1001311, /**< For rotary sliders, this colour fills the outer curve. */
  605. rotarySliderOutlineColourId = 0x1001312, /**< For rotary sliders, this colour is used to draw the outer curve's outline. */
  606. textBoxTextColourId = 0x1001400, /**< The colour for the text in the text-editor box used for editing the value. */
  607. textBoxBackgroundColourId = 0x1001500, /**< The background colour for the text-editor box. */
  608. textBoxHighlightColourId = 0x1001600, /**< The text highlight colour for the text-editor box. */
  609. textBoxOutlineColourId = 0x1001700 /**< The colour to use for a border around the text-editor box. */
  610. };
  611. protected:
  612. //==============================================================================
  613. /** @internal */
  614. void paint (Graphics&);
  615. /** @internal */
  616. void resized();
  617. /** @internal */
  618. void mouseDown (const MouseEvent&);
  619. /** @internal */
  620. void mouseUp (const MouseEvent&);
  621. /** @internal */
  622. void mouseDrag (const MouseEvent&);
  623. /** @internal */
  624. void mouseDoubleClick (const MouseEvent&);
  625. /** @internal */
  626. void mouseWheelMove (const MouseEvent&, const MouseWheelDetails&);
  627. /** @internal */
  628. void modifierKeysChanged (const ModifierKeys&);
  629. /** @internal */
  630. void lookAndFeelChanged();
  631. /** @internal */
  632. void enablementChanged();
  633. /** @internal */
  634. void focusOfChildComponentChanged (FocusChangeType);
  635. /** @internal */
  636. void colourChanged();
  637. /** Returns the best number of decimal places to use when displaying numbers.
  638. This is calculated from the slider's interval setting.
  639. */
  640. int getNumDecimalPlacesToDisplay() const noexcept;
  641. private:
  642. //==============================================================================
  643. class Pimpl;
  644. friend class Pimpl;
  645. friend class ScopedPointer<Pimpl>;
  646. ScopedPointer<Pimpl> pimpl;
  647. void init (SliderStyle, TextEntryBoxPosition);
  648. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Slider);
  649. };
  650. /** This typedef is just for compatibility with old code - newer code should use the Slider::Listener class directly. */
  651. typedef Slider::Listener SliderListener;
  652. #endif // __JUCE_SLIDER_JUCEHEADER__