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.

887 lines
41KB

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