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.

1565 lines
57KB

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