Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_Slider.h 46KB

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