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.

932 lines
43KB

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