The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

1598 lines
58KB

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