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.

1600 lines
58KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class Slider::Pimpl : public AsyncUpdater,
  18. public ButtonListener, // (can't use Button::Listener due to idiotic VC2005 bug)
  19. public LabelListener,
  20. public ValueListener
  21. {
  22. public:
  23. Pimpl (Slider& s, SliderStyle sliderStyle, TextEntryBoxPosition textBoxPosition)
  24. : owner (s),
  25. style (sliderStyle),
  26. lastCurrentValue (0), lastValueMin (0), lastValueMax (0),
  27. minimum (0), maximum (10), interval (0), doubleClickReturnValue (0),
  28. skewFactor (1.0), velocityModeSensitivity (1.0),
  29. velocityModeOffset (0.0), velocityModeThreshold (1),
  30. rotaryStart (float_Pi * 1.2f),
  31. rotaryEnd (float_Pi * 2.8f),
  32. sliderRegionStart (0), sliderRegionSize (1), sliderBeingDragged (-1),
  33. pixelsForFullDragExtent (250),
  34. textBoxPos (textBoxPosition),
  35. numDecimalPlaces (7),
  36. textBoxWidth (80), textBoxHeight (20),
  37. incDecButtonMode (incDecButtonsNotDraggable),
  38. editableText (true),
  39. doubleClickToValue (false),
  40. isVelocityBased (false),
  41. userKeyOverridesVelocity (true),
  42. rotaryStop (true),
  43. incDecButtonsSideBySide (false),
  44. sendChangeOnlyOnRelease (false),
  45. popupDisplayEnabled (false),
  46. menuEnabled (false),
  47. useDragEvents (false),
  48. scrollWheelEnabled (true),
  49. snapsToMousePos (true),
  50. parentForPopupDisplay (nullptr)
  51. {
  52. }
  53. ~Pimpl()
  54. {
  55. currentValue.removeListener (this);
  56. valueMin.removeListener (this);
  57. valueMax.removeListener (this);
  58. popupDisplay = nullptr;
  59. }
  60. //==============================================================================
  61. void registerListeners()
  62. {
  63. currentValue.addListener (this);
  64. valueMin.addListener (this);
  65. valueMax.addListener (this);
  66. }
  67. bool isHorizontal() const noexcept
  68. {
  69. return style == LinearHorizontal
  70. || style == LinearBar
  71. || style == TwoValueHorizontal
  72. || style == ThreeValueHorizontal;
  73. }
  74. bool isVertical() const noexcept
  75. {
  76. return style == LinearVertical
  77. || style == LinearBarVertical
  78. || style == TwoValueVertical
  79. || style == ThreeValueVertical;
  80. }
  81. bool isRotary() const noexcept
  82. {
  83. return style == Rotary
  84. || style == RotaryHorizontalDrag
  85. || style == RotaryVerticalDrag
  86. || style == RotaryHorizontalVerticalDrag;
  87. }
  88. bool incDecDragDirectionIsHorizontal() const noexcept
  89. {
  90. return incDecButtonMode == incDecButtonsDraggable_Horizontal
  91. || (incDecButtonMode == incDecButtonsDraggable_AutoDirection && incDecButtonsSideBySide);
  92. }
  93. float getPositionOfValue (const double value) const
  94. {
  95. if (isHorizontal() || isVertical())
  96. return getLinearSliderPos (value);
  97. jassertfalse; // not a valid call on a slider that doesn't work linearly!
  98. return 0.0f;
  99. }
  100. void setRange (const double newMin, const double newMax, const double newInt)
  101. {
  102. if (minimum != newMin || maximum != newMax || interval != newInt)
  103. {
  104. minimum = newMin;
  105. maximum = newMax;
  106. interval = newInt;
  107. // figure out the number of DPs needed to display all values at this
  108. // interval setting.
  109. numDecimalPlaces = 7;
  110. if (newInt != 0)
  111. {
  112. int v = std::abs (roundToInt (newInt * 10000000));
  113. while ((v % 10) == 0)
  114. {
  115. --numDecimalPlaces;
  116. v /= 10;
  117. }
  118. }
  119. // keep the current values inside the new range..
  120. if (style != TwoValueHorizontal && style != TwoValueVertical)
  121. {
  122. setValue (getValue(), dontSendNotification);
  123. }
  124. else
  125. {
  126. setMinValue (getMinValue(), dontSendNotification, false);
  127. setMaxValue (getMaxValue(), dontSendNotification, false);
  128. }
  129. updateText();
  130. }
  131. }
  132. double getValue() const
  133. {
  134. // for a two-value style slider, you should use the getMinValue() and getMaxValue()
  135. // methods to get the two values.
  136. jassert (style != TwoValueHorizontal && style != TwoValueVertical);
  137. return currentValue.getValue();
  138. }
  139. void setValue (double newValue, const NotificationType notification)
  140. {
  141. // for a two-value style slider, you should use the setMinValue() and setMaxValue()
  142. // methods to set the two values.
  143. jassert (style != TwoValueHorizontal && style != TwoValueVertical);
  144. newValue = constrainedValue (newValue);
  145. if (style == ThreeValueHorizontal || style == ThreeValueVertical)
  146. {
  147. jassert ((double) valueMin.getValue() <= (double) valueMax.getValue());
  148. newValue = jlimit ((double) valueMin.getValue(),
  149. (double) valueMax.getValue(),
  150. newValue);
  151. }
  152. if (newValue != lastCurrentValue)
  153. {
  154. if (valueBox != nullptr)
  155. valueBox->hideEditor (true);
  156. lastCurrentValue = newValue;
  157. // (need to do this comparison because the Value will use equalsWithSameType to compare
  158. // the new and old values, so will generate unwanted change events if the type changes)
  159. if (currentValue != newValue)
  160. currentValue = newValue;
  161. updateText();
  162. owner.repaint();
  163. if (popupDisplay != nullptr)
  164. popupDisplay->updatePosition (owner.getTextFromValue (newValue));
  165. triggerChangeMessage (notification);
  166. }
  167. }
  168. void setMinValue (double newValue, const NotificationType notification,
  169. const bool allowNudgingOfOtherValues)
  170. {
  171. // The minimum value only applies to sliders that are in two- or three-value mode.
  172. jassert (style == TwoValueHorizontal || style == TwoValueVertical
  173. || style == ThreeValueHorizontal || style == ThreeValueVertical);
  174. newValue = constrainedValue (newValue);
  175. if (style == TwoValueHorizontal || style == TwoValueVertical)
  176. {
  177. if (allowNudgingOfOtherValues && newValue > (double) valueMax.getValue())
  178. setMaxValue (newValue, notification, false);
  179. newValue = jmin ((double) valueMax.getValue(), newValue);
  180. }
  181. else
  182. {
  183. if (allowNudgingOfOtherValues && newValue > lastCurrentValue)
  184. setValue (newValue, notification);
  185. newValue = jmin (lastCurrentValue, newValue);
  186. }
  187. if (lastValueMin != newValue)
  188. {
  189. lastValueMin = newValue;
  190. valueMin = newValue;
  191. owner.repaint();
  192. if (popupDisplay != nullptr)
  193. popupDisplay->updatePosition (owner.getTextFromValue (newValue));
  194. triggerChangeMessage (notification);
  195. }
  196. }
  197. void setMaxValue (double newValue, const NotificationType notification,
  198. const bool allowNudgingOfOtherValues)
  199. {
  200. // The maximum value only applies to sliders that are in two- or three-value mode.
  201. jassert (style == TwoValueHorizontal || style == TwoValueVertical
  202. || style == ThreeValueHorizontal || style == ThreeValueVertical);
  203. newValue = constrainedValue (newValue);
  204. if (style == TwoValueHorizontal || style == TwoValueVertical)
  205. {
  206. if (allowNudgingOfOtherValues && newValue < (double) valueMin.getValue())
  207. setMinValue (newValue, notification, false);
  208. newValue = jmax ((double) valueMin.getValue(), newValue);
  209. }
  210. else
  211. {
  212. if (allowNudgingOfOtherValues && newValue < lastCurrentValue)
  213. setValue (newValue, notification);
  214. newValue = jmax (lastCurrentValue, newValue);
  215. }
  216. if (lastValueMax != newValue)
  217. {
  218. lastValueMax = newValue;
  219. valueMax = newValue;
  220. owner.repaint();
  221. if (popupDisplay != nullptr)
  222. popupDisplay->updatePosition (owner.getTextFromValue (valueMax.getValue()));
  223. triggerChangeMessage (notification);
  224. }
  225. }
  226. void setMinAndMaxValues (double newMinValue, double newMaxValue, const NotificationType notification)
  227. {
  228. // The maximum value only applies to sliders that are in two- or three-value mode.
  229. jassert (style == TwoValueHorizontal || style == TwoValueVertical
  230. || style == ThreeValueHorizontal || style == ThreeValueVertical);
  231. if (newMaxValue < newMinValue)
  232. std::swap (newMaxValue, newMinValue);
  233. newMinValue = constrainedValue (newMinValue);
  234. newMaxValue = constrainedValue (newMaxValue);
  235. if (lastValueMax != newMaxValue || lastValueMin != newMinValue)
  236. {
  237. lastValueMax = newMaxValue;
  238. lastValueMin = newMinValue;
  239. valueMin = newMinValue;
  240. valueMax = newMaxValue;
  241. owner.repaint();
  242. triggerChangeMessage (notification);
  243. }
  244. }
  245. double getMinValue() const
  246. {
  247. // The minimum value only applies to sliders that are in two- or three-value mode.
  248. jassert (style == TwoValueHorizontal || style == TwoValueVertical
  249. || style == ThreeValueHorizontal || style == ThreeValueVertical);
  250. return valueMin.getValue();
  251. }
  252. double getMaxValue() const
  253. {
  254. // The maximum value only applies to sliders that are in two- or three-value mode.
  255. jassert (style == TwoValueHorizontal || style == TwoValueVertical
  256. || style == ThreeValueHorizontal || style == ThreeValueVertical);
  257. return valueMax.getValue();
  258. }
  259. void triggerChangeMessage (const NotificationType notification)
  260. {
  261. if (notification != dontSendNotification)
  262. {
  263. if (notification == sendNotificationSync)
  264. handleAsyncUpdate();
  265. else
  266. triggerAsyncUpdate();
  267. owner.valueChanged();
  268. }
  269. }
  270. void handleAsyncUpdate() override
  271. {
  272. cancelPendingUpdate();
  273. Component::BailOutChecker checker (&owner);
  274. Slider* slider = &owner; // (must use an intermediate variable here to avoid a VS2005 compiler bug)
  275. listeners.callChecked (checker, &SliderListener::sliderValueChanged, slider); // (can't use Slider::Listener due to idiotic VC2005 bug)
  276. }
  277. void sendDragStart()
  278. {
  279. owner.startedDragging();
  280. Component::BailOutChecker checker (&owner);
  281. Slider* slider = &owner; // (must use an intermediate variable here to avoid a VS2005 compiler bug)
  282. listeners.callChecked (checker, &SliderListener::sliderDragStarted, slider);
  283. }
  284. void sendDragEnd()
  285. {
  286. owner.stoppedDragging();
  287. sliderBeingDragged = -1;
  288. Component::BailOutChecker checker (&owner);
  289. Slider* slider = &owner; // (must use an intermediate variable here to avoid a VS2005 compiler bug)
  290. listeners.callChecked (checker, &SliderListener::sliderDragEnded, slider);
  291. }
  292. struct DragInProgress
  293. {
  294. DragInProgress (Pimpl& p) : owner (p) { owner.sendDragStart(); }
  295. ~DragInProgress() { owner.sendDragEnd(); }
  296. Pimpl& owner;
  297. JUCE_DECLARE_NON_COPYABLE (DragInProgress)
  298. };
  299. void buttonClicked (Button* button) override
  300. {
  301. if (style == IncDecButtons)
  302. {
  303. const double delta = (button == incButton) ? interval : -interval;
  304. DragInProgress drag (*this);
  305. setValue (owner.snapValue (getValue() + delta, notDragging), sendNotificationSync);
  306. }
  307. }
  308. void valueChanged (Value& value) override
  309. {
  310. if (value.refersToSameSourceAs (currentValue))
  311. {
  312. if (style != TwoValueHorizontal && style != TwoValueVertical)
  313. setValue (currentValue.getValue(), dontSendNotification);
  314. }
  315. else if (value.refersToSameSourceAs (valueMin))
  316. setMinValue (valueMin.getValue(), dontSendNotification, true);
  317. else if (value.refersToSameSourceAs (valueMax))
  318. setMaxValue (valueMax.getValue(), dontSendNotification, true);
  319. }
  320. void labelTextChanged (Label* label) override
  321. {
  322. const double newValue = owner.snapValue (owner.getValueFromText (label->getText()), notDragging);
  323. if (newValue != (double) currentValue.getValue())
  324. {
  325. DragInProgress drag (*this);
  326. setValue (newValue, sendNotificationSync);
  327. }
  328. updateText(); // force a clean-up of the text, needed in case setValue() hasn't done this.
  329. }
  330. void updateText()
  331. {
  332. if (valueBox != nullptr)
  333. {
  334. String newValue (owner.getTextFromValue (currentValue.getValue()));
  335. if (newValue != valueBox->getText())
  336. valueBox->setText (newValue, dontSendNotification);
  337. }
  338. }
  339. double constrainedValue (double value) const
  340. {
  341. if (interval > 0)
  342. value = minimum + interval * std::floor ((value - minimum) / interval + 0.5);
  343. if (value <= minimum || maximum <= minimum)
  344. value = minimum;
  345. else if (value >= maximum)
  346. value = maximum;
  347. return value;
  348. }
  349. float getLinearSliderPos (const double value) const
  350. {
  351. double pos;
  352. if (maximum <= minimum)
  353. pos = 0.5;
  354. else if (value < minimum)
  355. pos = 0.0;
  356. else if (value > maximum)
  357. pos = 1.0;
  358. else
  359. pos = owner.valueToProportionOfLength (value);
  360. if (isVertical() || style == IncDecButtons)
  361. pos = 1.0 - pos;
  362. jassert (pos >= 0 && pos <= 1.0);
  363. return (float) (sliderRegionStart + pos * sliderRegionSize);
  364. }
  365. void setSliderStyle (const SliderStyle newStyle)
  366. {
  367. if (style != newStyle)
  368. {
  369. style = newStyle;
  370. owner.repaint();
  371. owner.lookAndFeelChanged();
  372. }
  373. }
  374. void setRotaryParameters (const float startAngleRadians,
  375. const float endAngleRadians,
  376. const bool stopAtEnd)
  377. {
  378. // make sure the values are sensible..
  379. jassert (rotaryStart >= 0 && rotaryEnd >= 0);
  380. jassert (rotaryStart < float_Pi * 4.0f && rotaryEnd < float_Pi * 4.0f);
  381. jassert (rotaryStart < rotaryEnd);
  382. rotaryStart = startAngleRadians;
  383. rotaryEnd = endAngleRadians;
  384. rotaryStop = stopAtEnd;
  385. }
  386. void setVelocityModeParameters (const double sensitivity, const int threshold,
  387. const double offset, const bool userCanPressKeyToSwapMode)
  388. {
  389. velocityModeSensitivity = sensitivity;
  390. velocityModeOffset = offset;
  391. velocityModeThreshold = threshold;
  392. userKeyOverridesVelocity = userCanPressKeyToSwapMode;
  393. }
  394. void setSkewFactorFromMidPoint (const double sliderValueToShowAtMidPoint)
  395. {
  396. if (maximum > minimum)
  397. skewFactor = log (0.5) / log ((sliderValueToShowAtMidPoint - minimum)
  398. / (maximum - minimum));
  399. }
  400. void setIncDecButtonsMode (const IncDecButtonMode mode)
  401. {
  402. if (incDecButtonMode != mode)
  403. {
  404. incDecButtonMode = mode;
  405. owner.lookAndFeelChanged();
  406. }
  407. }
  408. void setTextBoxStyle (const TextEntryBoxPosition newPosition,
  409. const bool isReadOnly,
  410. const int textEntryBoxWidth,
  411. const int textEntryBoxHeight)
  412. {
  413. if (textBoxPos != newPosition
  414. || editableText != (! isReadOnly)
  415. || textBoxWidth != textEntryBoxWidth
  416. || textBoxHeight != textEntryBoxHeight)
  417. {
  418. textBoxPos = newPosition;
  419. editableText = ! isReadOnly;
  420. textBoxWidth = textEntryBoxWidth;
  421. textBoxHeight = textEntryBoxHeight;
  422. owner.repaint();
  423. owner.lookAndFeelChanged();
  424. }
  425. }
  426. void setTextBoxIsEditable (const bool shouldBeEditable)
  427. {
  428. editableText = shouldBeEditable;
  429. if (valueBox != nullptr)
  430. valueBox->setEditable (shouldBeEditable && owner.isEnabled());
  431. }
  432. void showTextBox()
  433. {
  434. jassert (editableText); // this should probably be avoided in read-only sliders.
  435. if (valueBox != nullptr)
  436. valueBox->showEditor();
  437. }
  438. void hideTextBox (const bool discardCurrentEditorContents)
  439. {
  440. if (valueBox != nullptr)
  441. {
  442. valueBox->hideEditor (discardCurrentEditorContents);
  443. if (discardCurrentEditorContents)
  444. updateText();
  445. }
  446. }
  447. void setTextValueSuffix (const String& suffix)
  448. {
  449. if (textSuffix != suffix)
  450. {
  451. textSuffix = suffix;
  452. updateText();
  453. }
  454. }
  455. void lookAndFeelChanged (LookAndFeel& lf)
  456. {
  457. if (textBoxPos != NoTextBox)
  458. {
  459. const String previousTextBoxContent (valueBox != nullptr ? valueBox->getText()
  460. : owner.getTextFromValue (currentValue.getValue()));
  461. valueBox = nullptr;
  462. owner.addAndMakeVisible (valueBox = lf.createSliderTextBox (owner));
  463. valueBox->setWantsKeyboardFocus (false);
  464. valueBox->setText (previousTextBoxContent, dontSendNotification);
  465. if (valueBox->isEditable() != editableText) // (avoid overriding the single/double click flags unless we have to)
  466. valueBox->setEditable (editableText && owner.isEnabled());
  467. valueBox->addListener (this);
  468. if (style == LinearBar || style == LinearBarVertical)
  469. {
  470. valueBox->addMouseListener (&owner, false);
  471. valueBox->setMouseCursor (MouseCursor::ParentCursor);
  472. }
  473. else
  474. {
  475. valueBox->setTooltip (owner.getTooltip());
  476. }
  477. }
  478. else
  479. {
  480. valueBox = nullptr;
  481. }
  482. if (style == IncDecButtons)
  483. {
  484. owner.addAndMakeVisible (incButton = lf.createSliderButton (owner, true));
  485. incButton->addListener (this);
  486. owner.addAndMakeVisible (decButton = lf.createSliderButton (owner, false));
  487. decButton->addListener (this);
  488. if (incDecButtonMode != incDecButtonsNotDraggable)
  489. {
  490. incButton->addMouseListener (&owner, false);
  491. decButton->addMouseListener (&owner, false);
  492. }
  493. else
  494. {
  495. incButton->setRepeatSpeed (300, 100, 20);
  496. decButton->setRepeatSpeed (300, 100, 20);
  497. }
  498. const String tooltip (owner.getTooltip());
  499. incButton->setTooltip (tooltip);
  500. decButton->setTooltip (tooltip);
  501. }
  502. else
  503. {
  504. incButton = nullptr;
  505. decButton = nullptr;
  506. }
  507. owner.setComponentEffect (lf.getSliderEffect (owner));
  508. owner.resized();
  509. owner.repaint();
  510. }
  511. void showPopupMenu()
  512. {
  513. PopupMenu m;
  514. m.setLookAndFeel (&owner.getLookAndFeel());
  515. m.addItem (1, TRANS ("Velocity-sensitive mode"), true, isVelocityBased);
  516. m.addSeparator();
  517. if (isRotary())
  518. {
  519. PopupMenu rotaryMenu;
  520. rotaryMenu.addItem (2, TRANS ("Use circular dragging"), true, style == Rotary);
  521. rotaryMenu.addItem (3, TRANS ("Use left-right dragging"), true, style == RotaryHorizontalDrag);
  522. rotaryMenu.addItem (4, TRANS ("Use up-down dragging"), true, style == RotaryVerticalDrag);
  523. rotaryMenu.addItem (5, TRANS ("Use left-right/up-down dragging"), true, style == RotaryHorizontalVerticalDrag);
  524. m.addSubMenu (TRANS ("Rotary mode"), rotaryMenu);
  525. }
  526. m.showMenuAsync (PopupMenu::Options(),
  527. ModalCallbackFunction::forComponent (sliderMenuCallback, &owner));
  528. }
  529. static void sliderMenuCallback (const int result, Slider* slider)
  530. {
  531. if (slider != nullptr)
  532. {
  533. switch (result)
  534. {
  535. case 1: slider->setVelocityBasedMode (! slider->getVelocityBasedMode()); break;
  536. case 2: slider->setSliderStyle (Rotary); break;
  537. case 3: slider->setSliderStyle (RotaryHorizontalDrag); break;
  538. case 4: slider->setSliderStyle (RotaryVerticalDrag); break;
  539. case 5: slider->setSliderStyle (RotaryHorizontalVerticalDrag); break;
  540. default: break;
  541. }
  542. }
  543. }
  544. int getThumbIndexAt (const MouseEvent& e)
  545. {
  546. const bool isTwoValue = (style == TwoValueHorizontal || style == TwoValueVertical);
  547. const bool isThreeValue = (style == ThreeValueHorizontal || style == ThreeValueVertical);
  548. if (isTwoValue || isThreeValue)
  549. {
  550. const float mousePos = (float) (isVertical() ? e.y : e.x);
  551. const float normalPosDistance = std::abs (getLinearSliderPos (currentValue.getValue()) - mousePos);
  552. const float minPosDistance = std::abs (getLinearSliderPos (valueMin.getValue()) - 0.1f - mousePos);
  553. const float maxPosDistance = std::abs (getLinearSliderPos (valueMax.getValue()) + 0.1f - mousePos);
  554. if (isTwoValue)
  555. return maxPosDistance <= minPosDistance ? 2 : 1;
  556. if (normalPosDistance >= minPosDistance && maxPosDistance >= minPosDistance)
  557. return 1;
  558. if (normalPosDistance >= maxPosDistance)
  559. return 2;
  560. }
  561. return 0;
  562. }
  563. //==============================================================================
  564. void handleRotaryDrag (const MouseEvent& e)
  565. {
  566. const int dx = e.x - sliderRect.getCentreX();
  567. const int dy = e.y - sliderRect.getCentreY();
  568. if (dx * dx + dy * dy > 25)
  569. {
  570. double angle = std::atan2 ((double) dx, (double) -dy);
  571. while (angle < 0.0)
  572. angle += double_Pi * 2.0;
  573. if (rotaryStop && ! e.mouseWasClicked())
  574. {
  575. if (std::abs (angle - lastAngle) > double_Pi)
  576. {
  577. if (angle >= lastAngle)
  578. angle -= double_Pi * 2.0;
  579. else
  580. angle += double_Pi * 2.0;
  581. }
  582. if (angle >= lastAngle)
  583. angle = jmin (angle, (double) jmax (rotaryStart, rotaryEnd));
  584. else
  585. angle = jmax (angle, (double) jmin (rotaryStart, rotaryEnd));
  586. }
  587. else
  588. {
  589. while (angle < rotaryStart)
  590. angle += double_Pi * 2.0;
  591. if (angle > rotaryEnd)
  592. {
  593. if (smallestAngleBetween (angle, rotaryStart)
  594. <= smallestAngleBetween (angle, rotaryEnd))
  595. angle = rotaryStart;
  596. else
  597. angle = rotaryEnd;
  598. }
  599. }
  600. const double proportion = (angle - rotaryStart) / (rotaryEnd - rotaryStart);
  601. valueWhenLastDragged = owner.proportionOfLengthToValue (jlimit (0.0, 1.0, proportion));
  602. lastAngle = angle;
  603. }
  604. }
  605. void handleAbsoluteDrag (const MouseEvent& e)
  606. {
  607. const int mousePos = (isHorizontal() || style == RotaryHorizontalDrag) ? e.x : e.y;
  608. double newPos = (mousePos - sliderRegionStart) / (double) sliderRegionSize;
  609. if (style == RotaryHorizontalDrag
  610. || style == RotaryVerticalDrag
  611. || style == IncDecButtons
  612. || ((style == LinearHorizontal || style == LinearVertical || style == LinearBar || style == LinearBarVertical)
  613. && ! snapsToMousePos))
  614. {
  615. const int mouseDiff = (style == RotaryHorizontalDrag
  616. || style == LinearHorizontal
  617. || style == LinearBar
  618. || (style == IncDecButtons && incDecDragDirectionIsHorizontal()))
  619. ? e.x - mouseDragStartPos.x
  620. : mouseDragStartPos.y - e.y;
  621. newPos = owner.valueToProportionOfLength (valueOnMouseDown)
  622. + mouseDiff * (1.0 / pixelsForFullDragExtent);
  623. if (style == IncDecButtons)
  624. {
  625. incButton->setState (mouseDiff < 0 ? Button::buttonNormal : Button::buttonDown);
  626. decButton->setState (mouseDiff > 0 ? Button::buttonNormal : Button::buttonDown);
  627. }
  628. }
  629. else if (style == RotaryHorizontalVerticalDrag)
  630. {
  631. const int mouseDiff = (e.x - mouseDragStartPos.x) + (mouseDragStartPos.y - e.y);
  632. newPos = owner.valueToProportionOfLength (valueOnMouseDown)
  633. + mouseDiff * (1.0 / pixelsForFullDragExtent);
  634. }
  635. else
  636. {
  637. if (isVertical())
  638. newPos = 1.0 - newPos;
  639. }
  640. valueWhenLastDragged = owner.proportionOfLengthToValue (jlimit (0.0, 1.0, newPos));
  641. }
  642. void handleVelocityDrag (const MouseEvent& e)
  643. {
  644. const int mouseDiff = style == RotaryHorizontalVerticalDrag
  645. ? (e.x - mousePosWhenLastDragged.x) + (mousePosWhenLastDragged.y - e.y)
  646. : (isHorizontal()
  647. || style == RotaryHorizontalDrag
  648. || (style == IncDecButtons && incDecDragDirectionIsHorizontal()))
  649. ? e.x - mousePosWhenLastDragged.x
  650. : e.y - mousePosWhenLastDragged.y;
  651. const double maxSpeed = jmax (200, sliderRegionSize);
  652. double speed = jlimit (0.0, maxSpeed, (double) abs (mouseDiff));
  653. if (speed != 0)
  654. {
  655. speed = 0.2 * velocityModeSensitivity
  656. * (1.0 + std::sin (double_Pi * (1.5 + jmin (0.5, velocityModeOffset
  657. + jmax (0.0, (double) (speed - velocityModeThreshold))
  658. / maxSpeed))));
  659. if (mouseDiff < 0)
  660. speed = -speed;
  661. if (isVertical() || style == RotaryVerticalDrag
  662. || (style == IncDecButtons && ! incDecDragDirectionIsHorizontal()))
  663. speed = -speed;
  664. const double currentPos = owner.valueToProportionOfLength (valueWhenLastDragged);
  665. valueWhenLastDragged = owner.proportionOfLengthToValue (jlimit (0.0, 1.0, currentPos + speed));
  666. e.source.enableUnboundedMouseMovement (true, false);
  667. }
  668. }
  669. void mouseDown (const MouseEvent& e)
  670. {
  671. incDecDragged = false;
  672. useDragEvents = false;
  673. mouseDragStartPos = mousePosWhenLastDragged = e.getPosition();
  674. currentDrag = nullptr;
  675. if (owner.isEnabled())
  676. {
  677. if (e.mods.isPopupMenu() && menuEnabled)
  678. {
  679. showPopupMenu();
  680. }
  681. else if (canDoubleClickToValue() && e.mods.isAltDown())
  682. {
  683. mouseDoubleClick();
  684. }
  685. else if (maximum > minimum)
  686. {
  687. useDragEvents = true;
  688. if (valueBox != nullptr)
  689. valueBox->hideEditor (true);
  690. sliderBeingDragged = getThumbIndexAt (e);
  691. minMaxDiff = (double) valueMax.getValue() - (double) valueMin.getValue();
  692. lastAngle = rotaryStart + (rotaryEnd - rotaryStart)
  693. * owner.valueToProportionOfLength (currentValue.getValue());
  694. valueWhenLastDragged = (sliderBeingDragged == 2 ? valueMax
  695. : (sliderBeingDragged == 1 ? valueMin
  696. : currentValue)).getValue();
  697. valueOnMouseDown = valueWhenLastDragged;
  698. if (popupDisplayEnabled)
  699. {
  700. PopupDisplayComponent* const popup = new PopupDisplayComponent (owner);
  701. popupDisplay = popup;
  702. if (parentForPopupDisplay != nullptr)
  703. parentForPopupDisplay->addChildComponent (popup);
  704. else
  705. popup->addToDesktop (0);
  706. popup->setVisible (true);
  707. }
  708. currentDrag = new DragInProgress (*this);
  709. mouseDrag (e);
  710. }
  711. }
  712. }
  713. void mouseDrag (const MouseEvent& e)
  714. {
  715. if (useDragEvents && maximum > minimum
  716. && ! ((style == LinearBar || style == LinearBarVertical)
  717. && e.mouseWasClicked() && valueBox != nullptr && valueBox->isEditable()))
  718. {
  719. DragMode dragMode = notDragging;
  720. if (style == Rotary)
  721. {
  722. handleRotaryDrag (e);
  723. }
  724. else
  725. {
  726. if (style == IncDecButtons && ! incDecDragged)
  727. {
  728. if (e.getDistanceFromDragStart() < 10 || e.mouseWasClicked())
  729. return;
  730. incDecDragged = true;
  731. mouseDragStartPos = e.getPosition();
  732. }
  733. if (isAbsoluteDragMode (e.mods) || (maximum - minimum) / sliderRegionSize < interval)
  734. {
  735. dragMode = absoluteDrag;
  736. handleAbsoluteDrag (e);
  737. }
  738. else
  739. {
  740. dragMode = velocityDrag;
  741. handleVelocityDrag (e);
  742. }
  743. }
  744. valueWhenLastDragged = jlimit (minimum, maximum, valueWhenLastDragged);
  745. if (sliderBeingDragged == 0)
  746. {
  747. setValue (owner.snapValue (valueWhenLastDragged, dragMode),
  748. sendChangeOnlyOnRelease ? dontSendNotification : sendNotificationSync);
  749. }
  750. else if (sliderBeingDragged == 1)
  751. {
  752. setMinValue (owner.snapValue (valueWhenLastDragged, dragMode),
  753. sendChangeOnlyOnRelease ? dontSendNotification : sendNotificationAsync, true);
  754. if (e.mods.isShiftDown())
  755. setMaxValue (getMinValue() + minMaxDiff, dontSendNotification, true);
  756. else
  757. minMaxDiff = (double) valueMax.getValue() - (double) valueMin.getValue();
  758. }
  759. else if (sliderBeingDragged == 2)
  760. {
  761. setMaxValue (owner.snapValue (valueWhenLastDragged, dragMode),
  762. sendChangeOnlyOnRelease ? dontSendNotification : sendNotificationAsync, true);
  763. if (e.mods.isShiftDown())
  764. setMinValue (getMaxValue() - minMaxDiff, dontSendNotification, true);
  765. else
  766. minMaxDiff = (double) valueMax.getValue() - (double) valueMin.getValue();
  767. }
  768. mousePosWhenLastDragged = e.getPosition();
  769. }
  770. }
  771. void mouseUp()
  772. {
  773. if (owner.isEnabled()
  774. && useDragEvents
  775. && (maximum > minimum)
  776. && (style != IncDecButtons || incDecDragged))
  777. {
  778. restoreMouseIfHidden();
  779. if (sendChangeOnlyOnRelease && valueOnMouseDown != (double) currentValue.getValue())
  780. triggerChangeMessage (sendNotificationAsync);
  781. currentDrag = nullptr;
  782. popupDisplay = nullptr;
  783. if (style == IncDecButtons)
  784. {
  785. incButton->setState (Button::buttonNormal);
  786. decButton->setState (Button::buttonNormal);
  787. }
  788. }
  789. else if (popupDisplay != nullptr)
  790. {
  791. popupDisplay->startTimer (2000);
  792. }
  793. currentDrag = nullptr;
  794. }
  795. bool canDoubleClickToValue() const
  796. {
  797. return doubleClickToValue
  798. && style != IncDecButtons
  799. && minimum <= doubleClickReturnValue
  800. && maximum >= doubleClickReturnValue;
  801. }
  802. void mouseDoubleClick()
  803. {
  804. if (canDoubleClickToValue())
  805. {
  806. DragInProgress drag (*this);
  807. setValue (doubleClickReturnValue, sendNotificationSync);
  808. }
  809. }
  810. bool mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel)
  811. {
  812. if (scrollWheelEnabled
  813. && style != TwoValueHorizontal
  814. && style != TwoValueVertical)
  815. {
  816. if (maximum > minimum && ! e.mods.isAnyMouseButtonDown())
  817. {
  818. if (valueBox != nullptr)
  819. valueBox->hideEditor (false);
  820. const double value = (double) currentValue.getValue();
  821. const double proportionDelta = (wheel.deltaX != 0 ? -wheel.deltaX : wheel.deltaY)
  822. * (wheel.isReversed ? -0.15f : 0.15f);
  823. const double currentPos = owner.valueToProportionOfLength (value);
  824. const double newValue = owner.proportionOfLengthToValue (jlimit (0.0, 1.0, currentPos + proportionDelta));
  825. double delta = (newValue != value) ? jmax (std::abs (newValue - value), interval) : 0;
  826. if (value > newValue)
  827. delta = -delta;
  828. DragInProgress drag (*this);
  829. setValue (owner.snapValue (value + delta, notDragging), sendNotificationSync);
  830. }
  831. return true;
  832. }
  833. return false;
  834. }
  835. void modifierKeysChanged (const ModifierKeys& modifiers)
  836. {
  837. if (style != IncDecButtons && style != Rotary && isAbsoluteDragMode (modifiers))
  838. restoreMouseIfHidden();
  839. }
  840. bool isAbsoluteDragMode (ModifierKeys mods) const
  841. {
  842. return isVelocityBased == (userKeyOverridesVelocity
  843. && mods.testFlags (ModifierKeys::ctrlAltCommandModifiers));
  844. }
  845. void restoreMouseIfHidden()
  846. {
  847. const Array<MouseInputSource>& mouseSources = Desktop::getInstance().getMouseSources();
  848. for (MouseInputSource* mi = mouseSources.begin(), * const e = mouseSources.end(); mi != e; ++mi)
  849. {
  850. if (mi->isUnboundedMouseMovementEnabled())
  851. {
  852. mi->enableUnboundedMouseMovement (false);
  853. const double pos = sliderBeingDragged == 2 ? getMaxValue()
  854. : (sliderBeingDragged == 1 ? getMinValue()
  855. : (double) currentValue.getValue());
  856. Point<int> mousePos;
  857. if (isRotary())
  858. {
  859. mousePos = mi->getLastMouseDownPosition();
  860. const int delta = roundToInt (pixelsForFullDragExtent * (owner.valueToProportionOfLength (valueOnMouseDown)
  861. - owner.valueToProportionOfLength (pos)));
  862. if (style == RotaryHorizontalDrag) mousePos += Point<int> (-delta, 0);
  863. else if (style == RotaryVerticalDrag) mousePos += Point<int> (0, delta);
  864. else mousePos += Point<int> (delta / -2, delta / 2);
  865. mousePos = owner.getScreenBounds().reduced (4).getConstrainedPoint (mousePos);
  866. mouseDragStartPos = mousePosWhenLastDragged = owner.getLocalPoint (nullptr, mousePos);
  867. valueOnMouseDown = valueWhenLastDragged;
  868. }
  869. else
  870. {
  871. const int pixelPos = (int) getLinearSliderPos (pos);
  872. mousePos = owner.localPointToGlobal (Point<int> (isHorizontal() ? pixelPos : (owner.getWidth() / 2),
  873. isVertical() ? pixelPos : (owner.getHeight() / 2)));
  874. }
  875. mi->setScreenPosition (mousePos);
  876. }
  877. }
  878. }
  879. //==============================================================================
  880. void paint (Graphics& g, LookAndFeel& lf)
  881. {
  882. if (style != IncDecButtons)
  883. {
  884. if (isRotary())
  885. {
  886. const float sliderPos = (float) owner.valueToProportionOfLength (lastCurrentValue);
  887. jassert (sliderPos >= 0 && sliderPos <= 1.0f);
  888. lf.drawRotarySlider (g,
  889. sliderRect.getX(), sliderRect.getY(),
  890. sliderRect.getWidth(), sliderRect.getHeight(),
  891. sliderPos, rotaryStart, rotaryEnd, owner);
  892. }
  893. else
  894. {
  895. lf.drawLinearSlider (g,
  896. sliderRect.getX(), sliderRect.getY(),
  897. sliderRect.getWidth(), sliderRect.getHeight(),
  898. getLinearSliderPos (lastCurrentValue),
  899. getLinearSliderPos (lastValueMin),
  900. getLinearSliderPos (lastValueMax),
  901. style, owner);
  902. }
  903. if ((style == LinearBar || style == LinearBarVertical) && valueBox == nullptr)
  904. {
  905. g.setColour (owner.findColour (Slider::textBoxOutlineColourId));
  906. g.drawRect (0, 0, owner.getWidth(), owner.getHeight(), 1);
  907. }
  908. }
  909. }
  910. void resized (const Rectangle<int>& localBounds, LookAndFeel& lf)
  911. {
  912. int minXSpace = 0;
  913. int minYSpace = 0;
  914. if (textBoxPos == TextBoxLeft || textBoxPos == TextBoxRight)
  915. minXSpace = 30;
  916. else
  917. minYSpace = 15;
  918. const int tbw = jmax (0, jmin (textBoxWidth, localBounds.getWidth() - minXSpace));
  919. const int tbh = jmax (0, jmin (textBoxHeight, localBounds.getHeight() - minYSpace));
  920. if (style == LinearBar || style == LinearBarVertical)
  921. {
  922. if (valueBox != nullptr)
  923. valueBox->setBounds (localBounds);
  924. }
  925. else
  926. {
  927. if (textBoxPos == NoTextBox)
  928. {
  929. sliderRect = localBounds;
  930. }
  931. else if (textBoxPos == TextBoxLeft)
  932. {
  933. valueBox->setBounds (0, (localBounds.getHeight() - tbh) / 2, tbw, tbh);
  934. sliderRect.setBounds (tbw, 0, localBounds.getWidth() - tbw, localBounds.getHeight());
  935. }
  936. else if (textBoxPos == TextBoxRight)
  937. {
  938. valueBox->setBounds (localBounds.getWidth() - tbw, (localBounds.getHeight() - tbh) / 2, tbw, tbh);
  939. sliderRect.setBounds (0, 0, localBounds.getWidth() - tbw, localBounds.getHeight());
  940. }
  941. else if (textBoxPos == TextBoxAbove)
  942. {
  943. valueBox->setBounds ((localBounds.getWidth() - tbw) / 2, 0, tbw, tbh);
  944. sliderRect.setBounds (0, tbh, localBounds.getWidth(), localBounds.getHeight() - tbh);
  945. }
  946. else if (textBoxPos == TextBoxBelow)
  947. {
  948. valueBox->setBounds ((localBounds.getWidth() - tbw) / 2, localBounds.getHeight() - tbh, tbw, tbh);
  949. sliderRect.setBounds (0, 0, localBounds.getWidth(), localBounds.getHeight() - tbh);
  950. }
  951. }
  952. const int indent = lf.getSliderThumbRadius (owner);
  953. if (style == LinearBar)
  954. {
  955. const int barIndent = 1;
  956. sliderRegionStart = barIndent;
  957. sliderRegionSize = localBounds.getWidth() - barIndent * 2;
  958. sliderRect.setBounds (sliderRegionStart, barIndent,
  959. sliderRegionSize, localBounds.getHeight() - barIndent * 2);
  960. }
  961. else if (style == LinearBarVertical)
  962. {
  963. const int barIndent = 1;
  964. sliderRegionStart = barIndent;
  965. sliderRegionSize = localBounds.getHeight() - barIndent * 2;
  966. sliderRect.setBounds (barIndent, sliderRegionStart,
  967. localBounds.getWidth() - barIndent * 2, sliderRegionSize);
  968. }
  969. else if (isHorizontal())
  970. {
  971. sliderRegionStart = sliderRect.getX() + indent;
  972. sliderRegionSize = jmax (1, sliderRect.getWidth() - indent * 2);
  973. sliderRect.setBounds (sliderRegionStart, sliderRect.getY(),
  974. sliderRegionSize, sliderRect.getHeight());
  975. }
  976. else if (isVertical())
  977. {
  978. sliderRegionStart = sliderRect.getY() + indent;
  979. sliderRegionSize = jmax (1, sliderRect.getHeight() - indent * 2);
  980. sliderRect.setBounds (sliderRect.getX(), sliderRegionStart,
  981. sliderRect.getWidth(), sliderRegionSize);
  982. }
  983. else
  984. {
  985. sliderRegionStart = 0;
  986. sliderRegionSize = 100;
  987. }
  988. if (style == IncDecButtons)
  989. resizeIncDecButtons();
  990. }
  991. void resizeIncDecButtons()
  992. {
  993. Rectangle<int> buttonRect (sliderRect);
  994. if (textBoxPos == TextBoxLeft || textBoxPos == TextBoxRight)
  995. buttonRect.expand (-2, 0);
  996. else
  997. buttonRect.expand (0, -2);
  998. incDecButtonsSideBySide = buttonRect.getWidth() > buttonRect.getHeight();
  999. if (incDecButtonsSideBySide)
  1000. {
  1001. decButton->setBounds (buttonRect.removeFromLeft (buttonRect.getWidth() / 2));
  1002. decButton->setConnectedEdges (Button::ConnectedOnRight);
  1003. incButton->setConnectedEdges (Button::ConnectedOnLeft);
  1004. }
  1005. else
  1006. {
  1007. decButton->setBounds (buttonRect.removeFromBottom (buttonRect.getHeight() / 2));
  1008. decButton->setConnectedEdges (Button::ConnectedOnTop);
  1009. incButton->setConnectedEdges (Button::ConnectedOnBottom);
  1010. }
  1011. incButton->setBounds (buttonRect);
  1012. }
  1013. //==============================================================================
  1014. Slider& owner;
  1015. SliderStyle style;
  1016. ListenerList <SliderListener> listeners;
  1017. Value currentValue, valueMin, valueMax;
  1018. double lastCurrentValue, lastValueMin, lastValueMax;
  1019. double minimum, maximum, interval, doubleClickReturnValue;
  1020. double valueWhenLastDragged, valueOnMouseDown, skewFactor, lastAngle;
  1021. double velocityModeSensitivity, velocityModeOffset, minMaxDiff;
  1022. int velocityModeThreshold;
  1023. float rotaryStart, rotaryEnd;
  1024. Point<int> mouseDragStartPos, mousePosWhenLastDragged;
  1025. int sliderRegionStart, sliderRegionSize;
  1026. int sliderBeingDragged;
  1027. int pixelsForFullDragExtent;
  1028. Rectangle<int> sliderRect;
  1029. ScopedPointer<DragInProgress> currentDrag;
  1030. TextEntryBoxPosition textBoxPos;
  1031. String textSuffix;
  1032. int numDecimalPlaces;
  1033. int textBoxWidth, textBoxHeight;
  1034. IncDecButtonMode incDecButtonMode;
  1035. bool editableText;
  1036. bool doubleClickToValue;
  1037. bool isVelocityBased;
  1038. bool userKeyOverridesVelocity;
  1039. bool rotaryStop;
  1040. bool incDecButtonsSideBySide;
  1041. bool sendChangeOnlyOnRelease;
  1042. bool popupDisplayEnabled;
  1043. bool menuEnabled;
  1044. bool useDragEvents;
  1045. bool incDecDragged;
  1046. bool scrollWheelEnabled;
  1047. bool snapsToMousePos;
  1048. ScopedPointer<Label> valueBox;
  1049. ScopedPointer<Button> incButton, decButton;
  1050. //==============================================================================
  1051. class PopupDisplayComponent : public BubbleComponent,
  1052. public Timer
  1053. {
  1054. public:
  1055. PopupDisplayComponent (Slider& s)
  1056. : owner (s),
  1057. font (s.getLookAndFeel().getSliderPopupFont (s))
  1058. {
  1059. setAlwaysOnTop (true);
  1060. setAllowedPlacement (owner.getLookAndFeel().getSliderPopupPlacement (s));
  1061. }
  1062. void paintContent (Graphics& g, int w, int h)
  1063. {
  1064. g.setFont (font);
  1065. g.setColour (owner.findColour (TooltipWindow::textColourId, true));
  1066. g.drawFittedText (text, Rectangle<int> (w, h), Justification::centred, 1);
  1067. }
  1068. void getContentSize (int& w, int& h)
  1069. {
  1070. w = font.getStringWidth (text) + 18;
  1071. h = (int) (font.getHeight() * 1.6f);
  1072. }
  1073. void updatePosition (const String& newText)
  1074. {
  1075. text = newText;
  1076. BubbleComponent::setPosition (&owner);
  1077. repaint();
  1078. }
  1079. void timerCallback() override
  1080. {
  1081. owner.pimpl->popupDisplay = nullptr;
  1082. }
  1083. private:
  1084. Slider& owner;
  1085. Font font;
  1086. String text;
  1087. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PopupDisplayComponent)
  1088. };
  1089. ScopedPointer<PopupDisplayComponent> popupDisplay;
  1090. Component* parentForPopupDisplay;
  1091. //==============================================================================
  1092. static double smallestAngleBetween (const double a1, const double a2) noexcept
  1093. {
  1094. return jmin (std::abs (a1 - a2),
  1095. std::abs (a1 + double_Pi * 2.0 - a2),
  1096. std::abs (a2 + double_Pi * 2.0 - a1));
  1097. }
  1098. };
  1099. //==============================================================================
  1100. Slider::Slider()
  1101. {
  1102. init (LinearHorizontal, TextBoxLeft);
  1103. }
  1104. Slider::Slider (const String& name) : Component (name)
  1105. {
  1106. init (LinearHorizontal, TextBoxLeft);
  1107. }
  1108. Slider::Slider (SliderStyle style, TextEntryBoxPosition textBoxPos)
  1109. {
  1110. init (style, textBoxPos);
  1111. }
  1112. void Slider::init (SliderStyle style, TextEntryBoxPosition textBoxPos)
  1113. {
  1114. setWantsKeyboardFocus (false);
  1115. setRepaintsOnMouseActivity (true);
  1116. pimpl = new Pimpl (*this, style, textBoxPos);
  1117. Slider::lookAndFeelChanged();
  1118. updateText();
  1119. pimpl->registerListeners();
  1120. }
  1121. Slider::~Slider() {}
  1122. //==============================================================================
  1123. void Slider::addListener (SliderListener* const listener) { pimpl->listeners.add (listener); }
  1124. void Slider::removeListener (SliderListener* const listener) { pimpl->listeners.remove (listener); }
  1125. //==============================================================================
  1126. Slider::SliderStyle Slider::getSliderStyle() const noexcept { return pimpl->style; }
  1127. void Slider::setSliderStyle (const SliderStyle newStyle) { pimpl->setSliderStyle (newStyle); }
  1128. void Slider::setRotaryParameters (const float startAngleRadians, const float endAngleRadians, const bool stopAtEnd)
  1129. {
  1130. pimpl->setRotaryParameters (startAngleRadians, endAngleRadians, stopAtEnd);
  1131. }
  1132. void Slider::setVelocityBasedMode (bool vb) { pimpl->isVelocityBased = vb; }
  1133. bool Slider::getVelocityBasedMode() const noexcept { return pimpl->isVelocityBased; }
  1134. bool Slider::getVelocityModeIsSwappable() const noexcept { return pimpl->userKeyOverridesVelocity; }
  1135. int Slider::getVelocityThreshold() const noexcept { return pimpl->velocityModeThreshold; }
  1136. double Slider::getVelocitySensitivity() const noexcept { return pimpl->velocityModeSensitivity; }
  1137. double Slider::getVelocityOffset() const noexcept { return pimpl->velocityModeOffset; }
  1138. void Slider::setVelocityModeParameters (const double sensitivity, const int threshold,
  1139. const double offset, const bool userCanPressKeyToSwapMode)
  1140. {
  1141. jassert (threshold >= 0);
  1142. jassert (sensitivity > 0);
  1143. jassert (offset >= 0);
  1144. pimpl->setVelocityModeParameters (sensitivity, threshold, offset, userCanPressKeyToSwapMode);
  1145. }
  1146. double Slider::getSkewFactor() const noexcept { return pimpl->skewFactor; }
  1147. void Slider::setSkewFactor (const double factor) { pimpl->skewFactor = factor; }
  1148. void Slider::setSkewFactorFromMidPoint (const double sliderValueToShowAtMidPoint)
  1149. {
  1150. pimpl->setSkewFactorFromMidPoint (sliderValueToShowAtMidPoint);
  1151. }
  1152. int Slider::getMouseDragSensitivity() const noexcept { return pimpl->pixelsForFullDragExtent; }
  1153. void Slider::setMouseDragSensitivity (const int distanceForFullScaleDrag)
  1154. {
  1155. jassert (distanceForFullScaleDrag > 0);
  1156. pimpl->pixelsForFullDragExtent = distanceForFullScaleDrag;
  1157. }
  1158. void Slider::setIncDecButtonsMode (const IncDecButtonMode mode) { pimpl->setIncDecButtonsMode (mode); }
  1159. Slider::TextEntryBoxPosition Slider::getTextBoxPosition() const noexcept { return pimpl->textBoxPos; }
  1160. int Slider::getTextBoxWidth() const noexcept { return pimpl->textBoxWidth; }
  1161. int Slider::getTextBoxHeight() const noexcept { return pimpl->textBoxHeight; }
  1162. void Slider::setTextBoxStyle (const TextEntryBoxPosition newPosition, const bool isReadOnly,
  1163. const int textEntryBoxWidth, const int textEntryBoxHeight)
  1164. {
  1165. pimpl->setTextBoxStyle (newPosition, isReadOnly, textEntryBoxWidth, textEntryBoxHeight);
  1166. }
  1167. bool Slider::isTextBoxEditable() const noexcept { return pimpl->editableText; }
  1168. void Slider::setTextBoxIsEditable (const bool shouldBeEditable) { pimpl->setTextBoxIsEditable (shouldBeEditable); }
  1169. void Slider::showTextBox() { pimpl->showTextBox(); }
  1170. void Slider::hideTextBox (const bool discardCurrentEditorContents) { pimpl->hideTextBox (discardCurrentEditorContents); }
  1171. void Slider::setChangeNotificationOnlyOnRelease (bool onlyNotifyOnRelease)
  1172. {
  1173. pimpl->sendChangeOnlyOnRelease = onlyNotifyOnRelease;
  1174. }
  1175. bool Slider::getSliderSnapsToMousePosition() const noexcept { return pimpl->snapsToMousePos; }
  1176. void Slider::setSliderSnapsToMousePosition (const bool shouldSnapToMouse) { pimpl->snapsToMousePos = shouldSnapToMouse; }
  1177. void Slider::setPopupDisplayEnabled (const bool enabled, Component* const parentComponentToUse)
  1178. {
  1179. pimpl->popupDisplayEnabled = enabled;
  1180. pimpl->parentForPopupDisplay = parentComponentToUse;
  1181. }
  1182. Component* Slider::getCurrentPopupDisplay() const noexcept { return pimpl->popupDisplay.get(); }
  1183. //==============================================================================
  1184. void Slider::colourChanged() { lookAndFeelChanged(); }
  1185. void Slider::lookAndFeelChanged() { pimpl->lookAndFeelChanged (getLookAndFeel()); }
  1186. void Slider::enablementChanged() { repaint(); }
  1187. //==============================================================================
  1188. double Slider::getMaximum() const noexcept { return pimpl->maximum; }
  1189. double Slider::getMinimum() const noexcept { return pimpl->minimum; }
  1190. double Slider::getInterval() const noexcept { return pimpl->interval; }
  1191. void Slider::setRange (double newMin, double newMax, double newInt)
  1192. {
  1193. pimpl->setRange (newMin, newMax, newInt);
  1194. }
  1195. Value& Slider::getValueObject() noexcept { return pimpl->currentValue; }
  1196. Value& Slider::getMinValueObject() noexcept { return pimpl->valueMin; }
  1197. Value& Slider::getMaxValueObject() noexcept { return pimpl->valueMax; }
  1198. double Slider::getValue() const { return pimpl->getValue(); }
  1199. void Slider::setValue (double newValue, const NotificationType notification)
  1200. {
  1201. pimpl->setValue (newValue, notification);
  1202. }
  1203. double Slider::getMinValue() const { return pimpl->getMinValue(); }
  1204. double Slider::getMaxValue() const { return pimpl->getMaxValue(); }
  1205. void Slider::setMinValue (double newValue, const NotificationType notification, bool allowNudgingOfOtherValues)
  1206. {
  1207. pimpl->setMinValue (newValue, notification, allowNudgingOfOtherValues);
  1208. }
  1209. void Slider::setMaxValue (double newValue, const NotificationType notification, bool allowNudgingOfOtherValues)
  1210. {
  1211. pimpl->setMaxValue (newValue, notification, allowNudgingOfOtherValues);
  1212. }
  1213. void Slider::setMinAndMaxValues (double newMinValue, double newMaxValue, const NotificationType notification)
  1214. {
  1215. pimpl->setMinAndMaxValues (newMinValue, newMaxValue, notification);
  1216. }
  1217. void Slider::setDoubleClickReturnValue (bool isDoubleClickEnabled, double valueToSetOnDoubleClick)
  1218. {
  1219. pimpl->doubleClickToValue = isDoubleClickEnabled;
  1220. pimpl->doubleClickReturnValue = valueToSetOnDoubleClick;
  1221. }
  1222. double Slider::getDoubleClickReturnValue (bool& isEnabledResult) const
  1223. {
  1224. isEnabledResult = pimpl->doubleClickToValue;
  1225. return pimpl->doubleClickReturnValue;
  1226. }
  1227. void Slider::updateText()
  1228. {
  1229. pimpl->updateText();
  1230. }
  1231. void Slider::setTextValueSuffix (const String& suffix)
  1232. {
  1233. pimpl->setTextValueSuffix (suffix);
  1234. }
  1235. String Slider::getTextValueSuffix() const
  1236. {
  1237. return pimpl->textSuffix;
  1238. }
  1239. String Slider::getTextFromValue (double v)
  1240. {
  1241. if (getNumDecimalPlacesToDisplay() > 0)
  1242. return String (v, getNumDecimalPlacesToDisplay()) + getTextValueSuffix();
  1243. return String (roundToInt (v)) + getTextValueSuffix();
  1244. }
  1245. double Slider::getValueFromText (const String& text)
  1246. {
  1247. String t (text.trimStart());
  1248. if (t.endsWith (getTextValueSuffix()))
  1249. t = t.substring (0, t.length() - getTextValueSuffix().length());
  1250. while (t.startsWithChar ('+'))
  1251. t = t.substring (1).trimStart();
  1252. return t.initialSectionContainingOnly ("0123456789.,-")
  1253. .getDoubleValue();
  1254. }
  1255. double Slider::proportionOfLengthToValue (double proportion)
  1256. {
  1257. const double skew = getSkewFactor();
  1258. if (skew != 1.0 && proportion > 0.0)
  1259. proportion = exp (log (proportion) / skew);
  1260. return getMinimum() + (getMaximum() - getMinimum()) * proportion;
  1261. }
  1262. double Slider::valueToProportionOfLength (double value)
  1263. {
  1264. const double n = (value - getMinimum()) / (getMaximum() - getMinimum());
  1265. const double skew = getSkewFactor();
  1266. return skew == 1.0 ? n : pow (n, skew);
  1267. }
  1268. double Slider::snapValue (double attemptedValue, DragMode)
  1269. {
  1270. return attemptedValue;
  1271. }
  1272. int Slider::getNumDecimalPlacesToDisplay() const noexcept { return pimpl->numDecimalPlaces; }
  1273. //==============================================================================
  1274. int Slider::getThumbBeingDragged() const noexcept { return pimpl->sliderBeingDragged; }
  1275. void Slider::startedDragging() {}
  1276. void Slider::stoppedDragging() {}
  1277. void Slider::valueChanged() {}
  1278. //==============================================================================
  1279. void Slider::setPopupMenuEnabled (const bool menuEnabled) { pimpl->menuEnabled = menuEnabled; }
  1280. void Slider::setScrollWheelEnabled (const bool enabled) { pimpl->scrollWheelEnabled = enabled; }
  1281. bool Slider::isHorizontal() const noexcept { return pimpl->isHorizontal(); }
  1282. bool Slider::isVertical() const noexcept { return pimpl->isVertical(); }
  1283. float Slider::getPositionOfValue (const double value) { return pimpl->getPositionOfValue (value); }
  1284. //==============================================================================
  1285. void Slider::paint (Graphics& g) { pimpl->paint (g, getLookAndFeel()); }
  1286. void Slider::resized() { pimpl->resized (getLocalBounds(), getLookAndFeel()); }
  1287. void Slider::focusOfChildComponentChanged (FocusChangeType) { repaint(); }
  1288. void Slider::mouseDown (const MouseEvent& e) { pimpl->mouseDown (e); }
  1289. void Slider::mouseUp (const MouseEvent&) { pimpl->mouseUp(); }
  1290. void Slider::modifierKeysChanged (const ModifierKeys& modifiers)
  1291. {
  1292. if (isEnabled())
  1293. pimpl->modifierKeysChanged (modifiers);
  1294. }
  1295. void Slider::mouseDrag (const MouseEvent& e)
  1296. {
  1297. if (isEnabled())
  1298. pimpl->mouseDrag (e);
  1299. }
  1300. void Slider::mouseDoubleClick (const MouseEvent&)
  1301. {
  1302. if (isEnabled())
  1303. pimpl->mouseDoubleClick();
  1304. }
  1305. void Slider::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel)
  1306. {
  1307. if (! (isEnabled() && pimpl->mouseWheelMove (e, wheel)))
  1308. Component::mouseWheelMove (e, wheel);
  1309. }