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.

844 lines
38KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 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. #include "../../../events/juce_AsyncUpdater.h"
  23. #include "../../../containers/juce_Value.h"
  24. //==============================================================================
  25. /**
  26. A slider control for changing a value.
  27. The slider can be horizontal, vertical, or rotary, and can optionally have
  28. a text-box inside it to show an editable display of the current value.
  29. To use it, create a Slider object and use the setSliderStyle() method
  30. to set up the type you want. To set up the text-entry box, use setTextBoxStyle().
  31. To define the values that it can be set to, see the setRange() and setValue() methods.
  32. There are also lots of custom tweaks you can do by subclassing and overriding
  33. some of the virtual methods, such as changing the scaling, changing the format of
  34. the text display, custom ways of limiting the values, etc.
  35. You can register Slider::Listener objects with a slider, and they'll be called when
  36. the value changes.
  37. @see Slider::Listener
  38. */
  39. class JUCE_API Slider : public Component,
  40. public SettableTooltipClient,
  41. private AsyncUpdater,
  42. private Button::Listener,
  43. private Label::Listener,
  44. private Value::Listener
  45. {
  46. public:
  47. //==============================================================================
  48. /** Creates a slider.
  49. When created, you'll need to set up the slider's style and range with setSliderStyle(),
  50. setRange(), etc.
  51. */
  52. explicit Slider (const String& componentName = String::empty);
  53. /** Destructor. */
  54. ~Slider();
  55. //==============================================================================
  56. /** The types of slider available.
  57. @see setSliderStyle, setRotaryParameters
  58. */
  59. enum SliderStyle
  60. {
  61. LinearHorizontal, /**< A traditional horizontal slider. */
  62. LinearVertical, /**< A traditional vertical slider. */
  63. LinearBar, /**< A horizontal bar slider with the text label drawn on top of it. */
  64. Rotary, /**< A rotary control that you move by dragging the mouse in a circular motion, like a knob.
  65. @see setRotaryParameters */
  66. RotaryHorizontalDrag, /**< A rotary control that you move by dragging the mouse left-to-right.
  67. @see setRotaryParameters */
  68. RotaryVerticalDrag, /**< A rotary control that you move by dragging the mouse up-and-down.
  69. @see setRotaryParameters */
  70. IncDecButtons, /**< A pair of buttons that increment or decrement the slider's value by the increment set in setRange(). */
  71. TwoValueHorizontal, /**< A horizontal slider that has two thumbs instead of one, so it can show a minimum and maximum value.
  72. @see setMinValue, setMaxValue */
  73. TwoValueVertical, /**< A vertical slider that has two thumbs instead of one, so it can show a minimum and maximum value.
  74. @see setMinValue, setMaxValue */
  75. ThreeValueHorizontal, /**< A horizontal slider that has three thumbs instead of one, so it can show a minimum and maximum
  76. value, with the current value being somewhere between them.
  77. @see setMinValue, setMaxValue */
  78. ThreeValueVertical, /**< A vertical slider that has three thumbs instead of one, so it can show a minimum and maximum
  79. value, with the current value being somewhere between them.
  80. @see setMinValue, setMaxValue */
  81. };
  82. /** Changes the type of slider interface being used.
  83. @param newStyle the type of interface
  84. @see setRotaryParameters, setVelocityBasedMode,
  85. */
  86. void setSliderStyle (SliderStyle newStyle);
  87. /** Returns the slider's current style.
  88. @see setSliderStyle
  89. */
  90. SliderStyle getSliderStyle() const { return style; }
  91. //==============================================================================
  92. /** Changes the properties of a rotary slider.
  93. @param startAngleRadians the angle (in radians, clockwise from the top) at which
  94. the slider's minimum value is represented
  95. @param endAngleRadians the angle (in radians, clockwise from the top) at which
  96. the slider's maximum value is represented. This must be
  97. greater than startAngleRadians
  98. @param stopAtEnd if true, then when the slider is dragged around past the
  99. minimum or maximum, it'll stop there; if false, it'll wrap
  100. back to the opposite value
  101. */
  102. void setRotaryParameters (float startAngleRadians,
  103. float endAngleRadians,
  104. bool stopAtEnd);
  105. /** Sets the distance the mouse has to move to drag the slider across
  106. the full extent of its range.
  107. This only applies when in modes like RotaryHorizontalDrag, where it's using
  108. relative mouse movements to adjust the slider.
  109. */
  110. void setMouseDragSensitivity (int distanceForFullScaleDrag);
  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 { 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 { return velocityModeSensitivity; }
  142. /** Returns the velocity threshold setting.
  143. @see setVelocityModeParameters
  144. */
  145. int getVelocityThreshold() const { return velocityModeThreshold; }
  146. /** Returns the velocity offset setting.
  147. @see setVelocityModeParameters
  148. */
  149. double getVelocityOffset() const { return velocityModeOffset; }
  150. /** Returns the velocity user key setting.
  151. @see setVelocityModeParameters
  152. */
  153. bool getVelocityModeIsSwappable() const { 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 { 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 { return textBoxPos; }
  227. /** Returns the width used for the text-box.
  228. @see setTextBoxStyle
  229. */
  230. int getTextBoxWidth() const { return textBoxWidth; }
  231. /** Returns the height used for the text-box.
  232. @see setTextBoxStyle
  233. */
  234. int getTextBoxHeight() const { 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() { 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() { 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. //==============================================================================
  377. /** A class for receiving callbacks from a Slider.
  378. To be told when a slider's value changes, you can register a Slider::Listener
  379. object using Slider::addListener().
  380. @see Slider::addListener, Slider::removeListener
  381. */
  382. class JUCE_API Listener
  383. {
  384. public:
  385. //==============================================================================
  386. /** Destructor. */
  387. virtual ~Listener() {}
  388. //==============================================================================
  389. /** Called when the slider's value is changed.
  390. This may be caused by dragging it, or by typing in its text entry box,
  391. or by a call to Slider::setValue().
  392. You can find out the new value using Slider::getValue().
  393. @see Slider::valueChanged
  394. */
  395. virtual void sliderValueChanged (Slider* slider) = 0;
  396. //==============================================================================
  397. /** Called when the slider is about to be dragged.
  398. This is called when a drag begins, then it's followed by multiple calls
  399. to sliderValueChanged(), and then sliderDragEnded() is called after the
  400. user lets go.
  401. @see sliderDragEnded, Slider::startedDragging
  402. */
  403. virtual void sliderDragStarted (Slider* slider);
  404. /** Called after a drag operation has finished.
  405. @see sliderDragStarted, Slider::stoppedDragging
  406. */
  407. virtual void sliderDragEnded (Slider* slider);
  408. };
  409. /** Adds a listener to be called when this slider's value changes. */
  410. void addListener (Listener* listener);
  411. /** Removes a previously-registered listener. */
  412. void removeListener (Listener* listener);
  413. //==============================================================================
  414. /** This lets you choose whether double-clicking moves the slider to a given position.
  415. By default this is turned off, but it's handy if you want a double-click to act
  416. as a quick way of resetting a slider. Just pass in the value you want it to
  417. go to when double-clicked.
  418. @see getDoubleClickReturnValue
  419. */
  420. void setDoubleClickReturnValue (bool isDoubleClickEnabled,
  421. double valueToSetOnDoubleClick);
  422. /** Returns the values last set by setDoubleClickReturnValue() method.
  423. Sets isEnabled to true if double-click is enabled, and returns the value
  424. that was set.
  425. @see setDoubleClickReturnValue
  426. */
  427. double getDoubleClickReturnValue (bool& isEnabled) const;
  428. //==============================================================================
  429. /** Tells the slider whether to keep sending change messages while the user
  430. is dragging the slider.
  431. If set to true, a change message will only be sent when the user has
  432. dragged the slider and let go. If set to false (the default), then messages
  433. will be continuously sent as they drag it while the mouse button is still
  434. held down.
  435. */
  436. void setChangeNotificationOnlyOnRelease (bool onlyNotifyOnRelease);
  437. /** This lets you change whether the slider thumb jumps to the mouse position
  438. when you click.
  439. By default, this is true. If it's false, then the slider moves with relative
  440. motion when you drag it.
  441. This only applies to linear bars, and won't affect two- or three- value
  442. sliders.
  443. */
  444. void setSliderSnapsToMousePosition (bool shouldSnapToMouse);
  445. /** If enabled, this gives the slider a pop-up bubble which appears while the
  446. slider is being dragged.
  447. This can be handy if your slider doesn't have a text-box, so that users can
  448. see the value just when they're changing it.
  449. If you pass a component as the parentComponentToUse parameter, the pop-up
  450. bubble will be added as a child of that component when it's needed. If you
  451. pass 0, the pop-up will be placed on the desktop instead (note that it's a
  452. transparent window, so if you're using an OS that can't do transparent windows
  453. you'll have to add it to a parent component instead).
  454. */
  455. void setPopupDisplayEnabled (bool isEnabled,
  456. Component* parentComponentToUse);
  457. /** If this is set to true, then right-clicking on the slider will pop-up
  458. a menu to let the user change the way it works.
  459. By default this is turned off, but when turned on, the menu will include
  460. things like velocity sensitivity, and for rotary sliders, whether they
  461. use a linear or rotary mouse-drag to move them.
  462. */
  463. void setPopupMenuEnabled (bool menuEnabled);
  464. /** This can be used to stop the mouse scroll-wheel from moving the slider.
  465. By default it's enabled.
  466. */
  467. void setScrollWheelEnabled (bool enabled);
  468. /** Returns a number to indicate which thumb is currently being dragged by the
  469. mouse.
  470. This will return 0 for the main thumb, 1 for the minimum-value thumb, 2 for
  471. the maximum-value thumb, or -1 if none is currently down.
  472. */
  473. int getThumbBeingDragged() const { return sliderBeingDragged; }
  474. //==============================================================================
  475. /** Callback to indicate that the user is about to start dragging the slider.
  476. @see Slider::Listener::sliderDragStarted
  477. */
  478. virtual void startedDragging();
  479. /** Callback to indicate that the user has just stopped dragging the slider.
  480. @see Slider::Listener::sliderDragEnded
  481. */
  482. virtual void stoppedDragging();
  483. /** Callback to indicate that the user has just moved the slider.
  484. @see Slider::Listener::sliderValueChanged
  485. */
  486. virtual void valueChanged();
  487. //==============================================================================
  488. /** Subclasses can override this to convert a text string to a value.
  489. When the user enters something into the text-entry box, this method is
  490. called to convert it to a value.
  491. The default routine just tries to convert it to a double.
  492. @see getTextFromValue
  493. */
  494. virtual double getValueFromText (const String& text);
  495. /** Turns the slider's current value into a text string.
  496. Subclasses can override this to customise the formatting of the text-entry box.
  497. The default implementation just turns the value into a string, using
  498. a number of decimal places based on the range interval. If a suffix string
  499. has been set using setTextValueSuffix(), this will be appended to the text.
  500. @see getValueFromText
  501. */
  502. virtual const String getTextFromValue (double value);
  503. /** Sets a suffix to append to the end of the numeric value when it's displayed as
  504. a string.
  505. This is used by the default implementation of getTextFromValue(), and is just
  506. appended to the numeric value. For more advanced formatting, you can override
  507. getTextFromValue() and do something else.
  508. */
  509. void setTextValueSuffix (const String& suffix);
  510. /** Returns the suffix that was set by setTextValueSuffix(). */
  511. const String getTextValueSuffix() const;
  512. //==============================================================================
  513. /** Allows a user-defined mapping of distance along the slider to its value.
  514. The default implementation for this performs the skewing operation that
  515. can be set up in the setSkewFactor() method. Override it if you need
  516. some kind of custom mapping instead, but make sure you also implement the
  517. inverse function in valueToProportionOfLength().
  518. @param proportion a value 0 to 1.0, indicating a distance along the slider
  519. @returns the slider value that is represented by this position
  520. @see valueToProportionOfLength
  521. */
  522. virtual double proportionOfLengthToValue (double proportion);
  523. /** Allows a user-defined mapping of value to the position of the slider along its length.
  524. The default implementation for this performs the skewing operation that
  525. can be set up in the setSkewFactor() method. Override it if you need
  526. some kind of custom mapping instead, but make sure you also implement the
  527. inverse function in proportionOfLengthToValue().
  528. @param value a valid slider value, between the range of values specified in
  529. setRange()
  530. @returns a value 0 to 1.0 indicating the distance along the slider that
  531. represents this value
  532. @see proportionOfLengthToValue
  533. */
  534. virtual double valueToProportionOfLength (double value);
  535. /** Returns the X or Y coordinate of a value along the slider's length.
  536. If the slider is horizontal, this will be the X coordinate of the given
  537. value, relative to the left of the slider. If it's vertical, then this will
  538. be the Y coordinate, relative to the top of the slider.
  539. If the slider is rotary, this will throw an assertion and return 0. If the
  540. value is out-of-range, it will be constrained to the length of the slider.
  541. */
  542. float getPositionOfValue (double value);
  543. //==============================================================================
  544. /** This can be overridden to allow the slider to snap to user-definable values.
  545. If overridden, it will be called when the user tries to move the slider to
  546. a given position, and allows a subclass to sanity-check this value, possibly
  547. returning a different value to use instead.
  548. @param attemptedValue the value the user is trying to enter
  549. @param userIsDragging true if the user is dragging with the mouse; false if
  550. they are entering the value using the text box
  551. @returns the value to use instead
  552. */
  553. virtual double snapValue (double attemptedValue, bool userIsDragging);
  554. //==============================================================================
  555. /** This can be called to force the text box to update its contents.
  556. (Not normally needed, as this is done automatically).
  557. */
  558. void updateText();
  559. /** True if the slider moves horizontally. */
  560. bool isHorizontal() const;
  561. /** True if the slider moves vertically. */
  562. bool isVertical() const;
  563. //==============================================================================
  564. /** A set of colour IDs to use to change the colour of various aspects of the slider.
  565. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  566. methods.
  567. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  568. */
  569. enum ColourIds
  570. {
  571. backgroundColourId = 0x1001200, /**< A colour to use to fill the slider's background. */
  572. thumbColourId = 0x1001300, /**< The colour to draw the thumb with. It's up to the look
  573. and feel class how this is used. */
  574. trackColourId = 0x1001310, /**< The colour to draw the groove that the thumb moves along. */
  575. rotarySliderFillColourId = 0x1001311, /**< For rotary sliders, this colour fills the outer curve. */
  576. rotarySliderOutlineColourId = 0x1001312, /**< For rotary sliders, this colour is used to draw the outer curve's outline. */
  577. textBoxTextColourId = 0x1001400, /**< The colour for the text in the text-editor box used for editing the value. */
  578. textBoxBackgroundColourId = 0x1001500, /**< The background colour for the text-editor box. */
  579. textBoxHighlightColourId = 0x1001600, /**< The text highlight colour for the text-editor box. */
  580. textBoxOutlineColourId = 0x1001700 /**< The colour to use for a border around the text-editor box. */
  581. };
  582. //==============================================================================
  583. juce_UseDebuggingNewOperator
  584. protected:
  585. /** @internal */
  586. void labelTextChanged (Label*);
  587. /** @internal */
  588. void paint (Graphics& g);
  589. /** @internal */
  590. void resized();
  591. /** @internal */
  592. void mouseDown (const MouseEvent& e);
  593. /** @internal */
  594. void mouseUp (const MouseEvent& e);
  595. /** @internal */
  596. void mouseDrag (const MouseEvent& e);
  597. /** @internal */
  598. void mouseDoubleClick (const MouseEvent& e);
  599. /** @internal */
  600. void mouseWheelMove (const MouseEvent& e, float wheelIncrementX, float wheelIncrementY);
  601. /** @internal */
  602. void modifierKeysChanged (const ModifierKeys& modifiers);
  603. /** @internal */
  604. void buttonClicked (Button* button);
  605. /** @internal */
  606. void lookAndFeelChanged();
  607. /** @internal */
  608. void enablementChanged();
  609. /** @internal */
  610. void focusOfChildComponentChanged (FocusChangeType cause);
  611. /** @internal */
  612. void handleAsyncUpdate();
  613. /** @internal */
  614. void colourChanged();
  615. /** @internal */
  616. void valueChanged (Value& value);
  617. /** Returns the best number of decimal places to use when displaying numbers.
  618. This is calculated from the slider's interval setting.
  619. */
  620. int getNumDecimalPlacesToDisplay() const throw() { return numDecimalPlaces; }
  621. private:
  622. ListenerList <Listener> listeners;
  623. Value currentValue, valueMin, valueMax;
  624. double lastCurrentValue, lastValueMin, lastValueMax;
  625. double minimum, maximum, interval, doubleClickReturnValue;
  626. double valueWhenLastDragged, valueOnMouseDown, skewFactor, lastAngle;
  627. double velocityModeSensitivity, velocityModeOffset, minMaxDiff;
  628. int velocityModeThreshold;
  629. float rotaryStart, rotaryEnd;
  630. int numDecimalPlaces, mouseXWhenLastDragged, mouseYWhenLastDragged;
  631. int mouseDragStartX, mouseDragStartY;
  632. int sliderRegionStart, sliderRegionSize;
  633. int sliderBeingDragged;
  634. int pixelsForFullDragExtent;
  635. Rectangle<int> sliderRect;
  636. String textSuffix;
  637. SliderStyle style;
  638. TextEntryBoxPosition textBoxPos;
  639. int textBoxWidth, textBoxHeight;
  640. IncDecButtonMode incDecButtonMode;
  641. bool editableText : 1, doubleClickToValue : 1;
  642. bool isVelocityBased : 1, userKeyOverridesVelocity : 1, rotaryStop : 1;
  643. bool incDecButtonsSideBySide : 1, sendChangeOnlyOnRelease : 1, popupDisplayEnabled : 1;
  644. bool menuEnabled : 1, menuShown : 1, mouseWasHidden : 1, incDecDragged : 1;
  645. bool scrollWheelEnabled : 1, snapsToMousePos : 1;
  646. Label* valueBox;
  647. Button* incButton;
  648. Button* decButton;
  649. ScopedPointer <Component> popupDisplay;
  650. Component* parentForPopupDisplay;
  651. float getLinearSliderPos (double value);
  652. void restoreMouseIfHidden();
  653. void sendDragStart();
  654. void sendDragEnd();
  655. double constrainedValue (double value) const;
  656. void triggerChangeMessage (bool synchronous);
  657. bool incDecDragDirectionIsHorizontal() const;
  658. Slider (const Slider&);
  659. Slider& operator= (const Slider&);
  660. };
  661. /** This typedef is just for compatibility with old code - newer code should use the Slider::Listener class directly. */
  662. typedef Slider::Listener SliderListener;
  663. #endif // __JUCE_SLIDER_JUCEHEADER__