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.

926 lines
42KB

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