Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

897 lines
42KB

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