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.

799 lines
37KB

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