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.

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