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 43KB

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