juce_Slider.h 46 KB

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