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

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