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.

1569 lines
57KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. sliderRegionStart (0), sliderRegionSize (1), sliderBeingDragged (-1),
  31. pixelsForFullDragExtent (250),
  32. textBoxPos (textBoxPosition),
  33. numDecimalPlaces (7),
  34. textBoxWidth (80), textBoxHeight (20),
  35. incDecButtonMode (incDecButtonsNotDraggable),
  36. editableText (true),
  37. doubleClickToValue (false),
  38. isVelocityBased (false),
  39. userKeyOverridesVelocity (true),
  40. incDecButtonsSideBySide (false),
  41. sendChangeOnlyOnRelease (false),
  42. popupDisplayEnabled (false),
  43. menuEnabled (false),
  44. useDragEvents (false),
  45. scrollWheelEnabled (true),
  46. snapsToMousePos (true),
  47. parentForPopupDisplay (nullptr)
  48. {
  49. rotaryParams.startAngleRadians = float_Pi * 1.2f;
  50. rotaryParams.endAngleRadians = float_Pi * 2.8f;
  51. rotaryParams.stopAtEnd = true;
  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 setVelocityModeParameters (const double sensitivity, const int threshold,
  380. const double offset, const bool userCanPressKeyToSwapMode)
  381. {
  382. velocityModeSensitivity = sensitivity;
  383. velocityModeOffset = offset;
  384. velocityModeThreshold = threshold;
  385. userKeyOverridesVelocity = userCanPressKeyToSwapMode;
  386. }
  387. void setSkewFactorFromMidPoint (const double sliderValueToShowAtMidPoint)
  388. {
  389. if (maximum > minimum)
  390. skewFactor = log (0.5) / log ((sliderValueToShowAtMidPoint - minimum)
  391. / (maximum - minimum));
  392. }
  393. void setIncDecButtonsMode (const IncDecButtonMode mode)
  394. {
  395. if (incDecButtonMode != mode)
  396. {
  397. incDecButtonMode = mode;
  398. owner.lookAndFeelChanged();
  399. }
  400. }
  401. void setTextBoxStyle (const TextEntryBoxPosition newPosition,
  402. const bool isReadOnly,
  403. const int textEntryBoxWidth,
  404. const int textEntryBoxHeight)
  405. {
  406. if (textBoxPos != newPosition
  407. || editableText != (! isReadOnly)
  408. || textBoxWidth != textEntryBoxWidth
  409. || textBoxHeight != textEntryBoxHeight)
  410. {
  411. textBoxPos = newPosition;
  412. editableText = ! isReadOnly;
  413. textBoxWidth = textEntryBoxWidth;
  414. textBoxHeight = textEntryBoxHeight;
  415. owner.repaint();
  416. owner.lookAndFeelChanged();
  417. }
  418. }
  419. void setTextBoxIsEditable (const bool shouldBeEditable)
  420. {
  421. editableText = shouldBeEditable;
  422. updateTextBoxEnablement();
  423. }
  424. void showTextBox()
  425. {
  426. jassert (editableText); // this should probably be avoided in read-only sliders.
  427. if (valueBox != nullptr)
  428. valueBox->showEditor();
  429. }
  430. void hideTextBox (const bool discardCurrentEditorContents)
  431. {
  432. if (valueBox != nullptr)
  433. {
  434. valueBox->hideEditor (discardCurrentEditorContents);
  435. if (discardCurrentEditorContents)
  436. updateText();
  437. }
  438. }
  439. void setTextValueSuffix (const String& suffix)
  440. {
  441. if (textSuffix != suffix)
  442. {
  443. textSuffix = suffix;
  444. updateText();
  445. }
  446. }
  447. void updateTextBoxEnablement()
  448. {
  449. if (valueBox != nullptr)
  450. {
  451. const bool shouldBeEditable = editableText && owner.isEnabled();
  452. if (valueBox->isEditable() != shouldBeEditable) // (to avoid changing the single/double click flags unless we need to)
  453. valueBox->setEditable (shouldBeEditable);
  454. }
  455. }
  456. void lookAndFeelChanged (LookAndFeel& lf)
  457. {
  458. if (textBoxPos != NoTextBox)
  459. {
  460. const String previousTextBoxContent (valueBox != nullptr ? valueBox->getText()
  461. : owner.getTextFromValue (currentValue.getValue()));
  462. valueBox = nullptr;
  463. owner.addAndMakeVisible (valueBox = lf.createSliderTextBox (owner));
  464. valueBox->setWantsKeyboardFocus (false);
  465. valueBox->setText (previousTextBoxContent, dontSendNotification);
  466. valueBox->setTooltip (owner.getTooltip());
  467. updateTextBoxEnablement();
  468. valueBox->addListener (this);
  469. if (style == LinearBar || style == LinearBarVertical)
  470. {
  471. valueBox->addMouseListener (&owner, false);
  472. valueBox->setMouseCursor (MouseCursor::ParentCursor);
  473. }
  474. }
  475. else
  476. {
  477. valueBox = nullptr;
  478. }
  479. if (style == IncDecButtons)
  480. {
  481. owner.addAndMakeVisible (incButton = lf.createSliderButton (owner, true));
  482. incButton->addListener (this);
  483. owner.addAndMakeVisible (decButton = lf.createSliderButton (owner, false));
  484. decButton->addListener (this);
  485. if (incDecButtonMode != incDecButtonsNotDraggable)
  486. {
  487. incButton->addMouseListener (&owner, false);
  488. decButton->addMouseListener (&owner, false);
  489. }
  490. else
  491. {
  492. incButton->setRepeatSpeed (300, 100, 20);
  493. decButton->setRepeatSpeed (300, 100, 20);
  494. }
  495. const String tooltip (owner.getTooltip());
  496. incButton->setTooltip (tooltip);
  497. decButton->setTooltip (tooltip);
  498. }
  499. else
  500. {
  501. incButton = nullptr;
  502. decButton = nullptr;
  503. }
  504. owner.setComponentEffect (lf.getSliderEffect (owner));
  505. owner.resized();
  506. owner.repaint();
  507. }
  508. void showPopupMenu()
  509. {
  510. PopupMenu m;
  511. m.setLookAndFeel (&owner.getLookAndFeel());
  512. m.addItem (1, TRANS ("Velocity-sensitive mode"), true, isVelocityBased);
  513. m.addSeparator();
  514. if (isRotary())
  515. {
  516. PopupMenu rotaryMenu;
  517. rotaryMenu.addItem (2, TRANS ("Use circular dragging"), true, style == Rotary);
  518. rotaryMenu.addItem (3, TRANS ("Use left-right dragging"), true, style == RotaryHorizontalDrag);
  519. rotaryMenu.addItem (4, TRANS ("Use up-down dragging"), true, style == RotaryVerticalDrag);
  520. rotaryMenu.addItem (5, TRANS ("Use left-right/up-down dragging"), true, style == RotaryHorizontalVerticalDrag);
  521. m.addSubMenu (TRANS ("Rotary mode"), rotaryMenu);
  522. }
  523. m.showMenuAsync (PopupMenu::Options(),
  524. ModalCallbackFunction::forComponent (sliderMenuCallback, &owner));
  525. }
  526. static void sliderMenuCallback (const int result, Slider* slider)
  527. {
  528. if (slider != nullptr)
  529. {
  530. switch (result)
  531. {
  532. case 1: slider->setVelocityBasedMode (! slider->getVelocityBasedMode()); break;
  533. case 2: slider->setSliderStyle (Rotary); break;
  534. case 3: slider->setSliderStyle (RotaryHorizontalDrag); break;
  535. case 4: slider->setSliderStyle (RotaryVerticalDrag); break;
  536. case 5: slider->setSliderStyle (RotaryHorizontalVerticalDrag); break;
  537. default: break;
  538. }
  539. }
  540. }
  541. int getThumbIndexAt (const MouseEvent& e)
  542. {
  543. const bool isTwoValue = (style == TwoValueHorizontal || style == TwoValueVertical);
  544. const bool isThreeValue = (style == ThreeValueHorizontal || style == ThreeValueVertical);
  545. if (isTwoValue || isThreeValue)
  546. {
  547. const float mousePos = isVertical() ? e.position.y : e.position.x;
  548. const float normalPosDistance = std::abs (getLinearSliderPos (currentValue.getValue()) - mousePos);
  549. const float minPosDistance = std::abs (getLinearSliderPos (valueMin.getValue()) - 0.1f - mousePos);
  550. const float maxPosDistance = std::abs (getLinearSliderPos (valueMax.getValue()) + 0.1f - mousePos);
  551. if (isTwoValue)
  552. return maxPosDistance <= minPosDistance ? 2 : 1;
  553. if (normalPosDistance >= minPosDistance && maxPosDistance >= minPosDistance)
  554. return 1;
  555. if (normalPosDistance >= maxPosDistance)
  556. return 2;
  557. }
  558. return 0;
  559. }
  560. //==============================================================================
  561. void handleRotaryDrag (const MouseEvent& e)
  562. {
  563. const float dx = e.position.x - sliderRect.getCentreX();
  564. const float dy = e.position.y - sliderRect.getCentreY();
  565. if (dx * dx + dy * dy > 25.0f)
  566. {
  567. double angle = std::atan2 ((double) dx, (double) -dy);
  568. while (angle < 0.0)
  569. angle += double_Pi * 2.0;
  570. if (rotaryParams.stopAtEnd && e.mouseWasDraggedSinceMouseDown())
  571. {
  572. if (std::abs (angle - lastAngle) > double_Pi)
  573. {
  574. if (angle >= lastAngle)
  575. angle -= double_Pi * 2.0;
  576. else
  577. angle += double_Pi * 2.0;
  578. }
  579. if (angle >= lastAngle)
  580. angle = jmin (angle, (double) jmax (rotaryParams.startAngleRadians, rotaryParams.endAngleRadians));
  581. else
  582. angle = jmax (angle, (double) jmin (rotaryParams.startAngleRadians, rotaryParams.endAngleRadians));
  583. }
  584. else
  585. {
  586. while (angle < rotaryParams.startAngleRadians)
  587. angle += double_Pi * 2.0;
  588. if (angle > rotaryParams.endAngleRadians)
  589. {
  590. if (smallestAngleBetween (angle, rotaryParams.startAngleRadians)
  591. <= smallestAngleBetween (angle, rotaryParams.endAngleRadians))
  592. angle = rotaryParams.startAngleRadians;
  593. else
  594. angle = rotaryParams.endAngleRadians;
  595. }
  596. }
  597. const double proportion = (angle - rotaryParams.startAngleRadians) / (rotaryParams.endAngleRadians - rotaryParams.startAngleRadians);
  598. valueWhenLastDragged = owner.proportionOfLengthToValue (jlimit (0.0, 1.0, proportion));
  599. lastAngle = angle;
  600. }
  601. }
  602. void handleAbsoluteDrag (const MouseEvent& e)
  603. {
  604. const float mousePos = (isHorizontal() || style == RotaryHorizontalDrag) ? e.position.x : e.position.y;
  605. double newPos = 0;
  606. if (style == RotaryHorizontalDrag
  607. || style == RotaryVerticalDrag
  608. || style == IncDecButtons
  609. || ((style == LinearHorizontal || style == LinearVertical || style == LinearBar || style == LinearBarVertical)
  610. && ! snapsToMousePos))
  611. {
  612. const float mouseDiff = (style == RotaryHorizontalDrag
  613. || style == LinearHorizontal
  614. || style == LinearBar
  615. || (style == IncDecButtons && incDecDragDirectionIsHorizontal()))
  616. ? e.position.x - mouseDragStartPos.x
  617. : mouseDragStartPos.y - e.position.y;
  618. newPos = owner.valueToProportionOfLength (valueOnMouseDown)
  619. + mouseDiff * (1.0 / pixelsForFullDragExtent);
  620. if (style == IncDecButtons)
  621. {
  622. incButton->setState (mouseDiff < 0 ? Button::buttonNormal : Button::buttonDown);
  623. decButton->setState (mouseDiff > 0 ? Button::buttonNormal : Button::buttonDown);
  624. }
  625. }
  626. else if (style == RotaryHorizontalVerticalDrag)
  627. {
  628. const float mouseDiff = (e.position.x - mouseDragStartPos.x)
  629. + (mouseDragStartPos.y - e.position.y);
  630. newPos = owner.valueToProportionOfLength (valueOnMouseDown)
  631. + mouseDiff * (1.0 / pixelsForFullDragExtent);
  632. }
  633. else
  634. {
  635. newPos = (mousePos - sliderRegionStart) / (double) sliderRegionSize;
  636. if (isVertical())
  637. newPos = 1.0 - newPos;
  638. }
  639. valueWhenLastDragged = owner.proportionOfLengthToValue (jlimit (0.0, 1.0, newPos));
  640. }
  641. void handleVelocityDrag (const MouseEvent& e)
  642. {
  643. const float mouseDiff = style == RotaryHorizontalVerticalDrag
  644. ? (e.position.x - mousePosWhenLastDragged.x) + (mousePosWhenLastDragged.y - e.position.y)
  645. : (isHorizontal()
  646. || style == RotaryHorizontalDrag
  647. || (style == IncDecButtons && incDecDragDirectionIsHorizontal()))
  648. ? e.position.x - mousePosWhenLastDragged.x
  649. : e.position.y - mousePosWhenLastDragged.y;
  650. const double maxSpeed = jmax (200, sliderRegionSize);
  651. double speed = jlimit (0.0, maxSpeed, (double) std::abs (mouseDiff));
  652. if (speed != 0)
  653. {
  654. speed = 0.2 * velocityModeSensitivity
  655. * (1.0 + std::sin (double_Pi * (1.5 + jmin (0.5, velocityModeOffset
  656. + jmax (0.0, (double) (speed - velocityModeThreshold))
  657. / maxSpeed))));
  658. if (mouseDiff < 0)
  659. speed = -speed;
  660. if (isVertical() || style == RotaryVerticalDrag
  661. || (style == IncDecButtons && ! incDecDragDirectionIsHorizontal()))
  662. speed = -speed;
  663. const double currentPos = owner.valueToProportionOfLength (valueWhenLastDragged);
  664. valueWhenLastDragged = owner.proportionOfLengthToValue (jlimit (0.0, 1.0, currentPos + speed));
  665. e.source.enableUnboundedMouseMovement (true, false);
  666. }
  667. }
  668. void mouseDown (const MouseEvent& e)
  669. {
  670. incDecDragged = false;
  671. useDragEvents = false;
  672. mouseDragStartPos = mousePosWhenLastDragged = e.position;
  673. currentDrag = nullptr;
  674. if (owner.isEnabled())
  675. {
  676. if (e.mods.isPopupMenu() && menuEnabled)
  677. {
  678. showPopupMenu();
  679. }
  680. else if (canDoubleClickToValue()
  681. && e.mods.withoutMouseButtons() == ModifierKeys (ModifierKeys::altModifier))
  682. {
  683. mouseDoubleClick();
  684. }
  685. else if (maximum > minimum)
  686. {
  687. useDragEvents = true;
  688. if (valueBox != nullptr)
  689. valueBox->hideEditor (true);
  690. sliderBeingDragged = getThumbIndexAt (e);
  691. minMaxDiff = (double) valueMax.getValue() - (double) valueMin.getValue();
  692. lastAngle = rotaryParams.startAngleRadians
  693. + (rotaryParams.endAngleRadians - rotaryParams.startAngleRadians)
  694. * owner.valueToProportionOfLength (currentValue.getValue());
  695. valueWhenLastDragged = (sliderBeingDragged == 2 ? valueMax
  696. : (sliderBeingDragged == 1 ? valueMin
  697. : currentValue)).getValue();
  698. valueOnMouseDown = valueWhenLastDragged;
  699. if (popupDisplayEnabled)
  700. {
  701. PopupDisplayComponent* const popup = new PopupDisplayComponent (owner);
  702. popupDisplay = popup;
  703. if (parentForPopupDisplay != nullptr)
  704. parentForPopupDisplay->addChildComponent (popup);
  705. else
  706. popup->addToDesktop (ComponentPeer::windowIsTemporary);
  707. popup->setVisible (true);
  708. }
  709. currentDrag = new DragInProgress (*this);
  710. mouseDrag (e);
  711. }
  712. }
  713. }
  714. void mouseDrag (const MouseEvent& e)
  715. {
  716. if (useDragEvents && maximum > minimum
  717. && ! ((style == LinearBar || style == LinearBarVertical)
  718. && e.mouseWasClicked() && valueBox != nullptr && valueBox->isEditable()))
  719. {
  720. DragMode dragMode = notDragging;
  721. if (style == Rotary)
  722. {
  723. handleRotaryDrag (e);
  724. }
  725. else
  726. {
  727. if (style == IncDecButtons && ! incDecDragged)
  728. {
  729. if (e.getDistanceFromDragStart() < 10 || ! e.mouseWasDraggedSinceMouseDown())
  730. return;
  731. incDecDragged = true;
  732. mouseDragStartPos = e.position;
  733. }
  734. if (isAbsoluteDragMode (e.mods) || (maximum - minimum) / sliderRegionSize < interval)
  735. {
  736. dragMode = absoluteDrag;
  737. handleAbsoluteDrag (e);
  738. }
  739. else
  740. {
  741. dragMode = velocityDrag;
  742. handleVelocityDrag (e);
  743. }
  744. }
  745. valueWhenLastDragged = jlimit (minimum, maximum, valueWhenLastDragged);
  746. if (sliderBeingDragged == 0)
  747. {
  748. setValue (owner.snapValue (valueWhenLastDragged, dragMode),
  749. sendChangeOnlyOnRelease ? dontSendNotification : sendNotificationSync);
  750. }
  751. else if (sliderBeingDragged == 1)
  752. {
  753. setMinValue (owner.snapValue (valueWhenLastDragged, dragMode),
  754. sendChangeOnlyOnRelease ? dontSendNotification : sendNotificationAsync, true);
  755. if (e.mods.isShiftDown())
  756. setMaxValue (getMinValue() + minMaxDiff, dontSendNotification, true);
  757. else
  758. minMaxDiff = (double) valueMax.getValue() - (double) valueMin.getValue();
  759. }
  760. else if (sliderBeingDragged == 2)
  761. {
  762. setMaxValue (owner.snapValue (valueWhenLastDragged, dragMode),
  763. sendChangeOnlyOnRelease ? dontSendNotification : sendNotificationAsync, true);
  764. if (e.mods.isShiftDown())
  765. setMinValue (getMaxValue() - minMaxDiff, dontSendNotification, true);
  766. else
  767. minMaxDiff = (double) valueMax.getValue() - (double) valueMin.getValue();
  768. }
  769. mousePosWhenLastDragged = e.position;
  770. }
  771. }
  772. void mouseUp()
  773. {
  774. if (owner.isEnabled()
  775. && useDragEvents
  776. && (maximum > minimum)
  777. && (style != IncDecButtons || incDecDragged))
  778. {
  779. restoreMouseIfHidden();
  780. if (sendChangeOnlyOnRelease && valueOnMouseDown != (double) currentValue.getValue())
  781. triggerChangeMessage (sendNotificationAsync);
  782. currentDrag = nullptr;
  783. popupDisplay = nullptr;
  784. if (style == IncDecButtons)
  785. {
  786. incButton->setState (Button::buttonNormal);
  787. decButton->setState (Button::buttonNormal);
  788. }
  789. }
  790. else if (popupDisplay != nullptr)
  791. {
  792. popupDisplay->startTimer (2000);
  793. }
  794. currentDrag = nullptr;
  795. }
  796. bool canDoubleClickToValue() const
  797. {
  798. return doubleClickToValue
  799. && style != IncDecButtons
  800. && minimum <= doubleClickReturnValue
  801. && maximum >= doubleClickReturnValue;
  802. }
  803. void mouseDoubleClick()
  804. {
  805. if (canDoubleClickToValue())
  806. {
  807. DragInProgress drag (*this);
  808. setValue (doubleClickReturnValue, sendNotificationSync);
  809. }
  810. }
  811. double getMouseWheelDelta (double value, double wheelAmount)
  812. {
  813. if (style == IncDecButtons)
  814. return interval * wheelAmount;
  815. const double proportionDelta = wheelAmount * 0.15f;
  816. const double currentPos = owner.valueToProportionOfLength (value);
  817. return owner.proportionOfLengthToValue (jlimit (0.0, 1.0, currentPos + proportionDelta)) - value;
  818. }
  819. bool mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel)
  820. {
  821. if (scrollWheelEnabled
  822. && style != TwoValueHorizontal
  823. && style != TwoValueVertical)
  824. {
  825. // sometimes duplicate wheel events seem to be sent, so since we're going to
  826. // bump the value by a minimum of the interval, avoid doing this twice..
  827. if (e.eventTime != lastMouseWheelTime)
  828. {
  829. lastMouseWheelTime = e.eventTime;
  830. if (maximum > minimum && ! e.mods.isAnyMouseButtonDown())
  831. {
  832. if (valueBox != nullptr)
  833. valueBox->hideEditor (false);
  834. const double value = (double) currentValue.getValue();
  835. const double delta = getMouseWheelDelta (value, (std::abs (wheel.deltaX) > std::abs (wheel.deltaY)
  836. ? -wheel.deltaX : wheel.deltaY)
  837. * (wheel.isReversed ? -1.0f : 1.0f));
  838. if (delta != 0)
  839. {
  840. const double newValue = value + jmax (interval, std::abs (delta)) * (delta < 0 ? -1.0 : 1.0);
  841. DragInProgress drag (*this);
  842. setValue (owner.snapValue (newValue, notDragging), sendNotificationSync);
  843. }
  844. }
  845. }
  846. return true;
  847. }
  848. return false;
  849. }
  850. void modifierKeysChanged (const ModifierKeys& modifiers)
  851. {
  852. if (style != IncDecButtons && style != Rotary && isAbsoluteDragMode (modifiers))
  853. restoreMouseIfHidden();
  854. }
  855. bool isAbsoluteDragMode (ModifierKeys mods) const
  856. {
  857. return isVelocityBased == (userKeyOverridesVelocity
  858. && mods.testFlags (ModifierKeys::ctrlAltCommandModifiers));
  859. }
  860. void restoreMouseIfHidden()
  861. {
  862. const Array<MouseInputSource>& mouseSources = Desktop::getInstance().getMouseSources();
  863. for (MouseInputSource* mi = mouseSources.begin(), * const e = mouseSources.end(); mi != e; ++mi)
  864. {
  865. if (mi->isUnboundedMouseMovementEnabled())
  866. {
  867. mi->enableUnboundedMouseMovement (false);
  868. const double pos = sliderBeingDragged == 2 ? getMaxValue()
  869. : (sliderBeingDragged == 1 ? getMinValue()
  870. : (double) currentValue.getValue());
  871. Point<float> mousePos;
  872. if (isRotary())
  873. {
  874. mousePos = mi->getLastMouseDownPosition();
  875. const float delta = (float) (pixelsForFullDragExtent * (owner.valueToProportionOfLength (valueOnMouseDown)
  876. - owner.valueToProportionOfLength (pos)));
  877. if (style == RotaryHorizontalDrag) mousePos += Point<float> (-delta, 0.0f);
  878. else if (style == RotaryVerticalDrag) mousePos += Point<float> (0.0f, delta);
  879. else mousePos += Point<float> (delta / -2.0f, delta / 2.0f);
  880. mousePos = owner.getScreenBounds().reduced (4).toFloat().getConstrainedPoint (mousePos);
  881. mouseDragStartPos = mousePosWhenLastDragged = owner.getLocalPoint (nullptr, mousePos);
  882. valueOnMouseDown = valueWhenLastDragged;
  883. }
  884. else
  885. {
  886. const float pixelPos = (float) getLinearSliderPos (pos);
  887. mousePos = owner.localPointToGlobal (Point<float> (isHorizontal() ? pixelPos : (owner.getWidth() / 2.0f),
  888. isVertical() ? pixelPos : (owner.getHeight() / 2.0f)));
  889. }
  890. mi->setScreenPosition (mousePos);
  891. }
  892. }
  893. }
  894. //==============================================================================
  895. void paint (Graphics& g, LookAndFeel& lf)
  896. {
  897. if (style != IncDecButtons)
  898. {
  899. if (isRotary())
  900. {
  901. const float sliderPos = (float) owner.valueToProportionOfLength (lastCurrentValue);
  902. jassert (sliderPos >= 0 && sliderPos <= 1.0f);
  903. lf.drawRotarySlider (g,
  904. sliderRect.getX(), sliderRect.getY(),
  905. sliderRect.getWidth(), sliderRect.getHeight(),
  906. sliderPos, rotaryParams.startAngleRadians,
  907. rotaryParams.endAngleRadians, owner);
  908. }
  909. else
  910. {
  911. lf.drawLinearSlider (g,
  912. sliderRect.getX(), sliderRect.getY(),
  913. sliderRect.getWidth(), sliderRect.getHeight(),
  914. getLinearSliderPos (lastCurrentValue),
  915. getLinearSliderPos (lastValueMin),
  916. getLinearSliderPos (lastValueMax),
  917. style, owner);
  918. }
  919. if ((style == LinearBar || style == LinearBarVertical) && valueBox == nullptr)
  920. {
  921. g.setColour (owner.findColour (Slider::textBoxOutlineColourId));
  922. g.drawRect (0, 0, owner.getWidth(), owner.getHeight(), 1);
  923. }
  924. }
  925. }
  926. //==============================================================================
  927. void resized (LookAndFeel& lf)
  928. {
  929. SliderLayout layout = lf.getSliderLayout (owner);
  930. sliderRect = layout.sliderBounds;
  931. if (valueBox != nullptr)
  932. valueBox->setBounds (layout.textBoxBounds);
  933. if (isHorizontal())
  934. {
  935. sliderRegionStart = layout.sliderBounds.getX();
  936. sliderRegionSize = layout.sliderBounds.getWidth();
  937. }
  938. else if (isVertical())
  939. {
  940. sliderRegionStart = layout.sliderBounds.getY();
  941. sliderRegionSize = layout.sliderBounds.getHeight();
  942. }
  943. else if (style == IncDecButtons)
  944. {
  945. resizeIncDecButtons();
  946. }
  947. }
  948. //==============================================================================
  949. void resizeIncDecButtons()
  950. {
  951. Rectangle<int> buttonRect (sliderRect);
  952. if (textBoxPos == TextBoxLeft || textBoxPos == TextBoxRight)
  953. buttonRect.expand (-2, 0);
  954. else
  955. buttonRect.expand (0, -2);
  956. incDecButtonsSideBySide = buttonRect.getWidth() > buttonRect.getHeight();
  957. if (incDecButtonsSideBySide)
  958. {
  959. decButton->setBounds (buttonRect.removeFromLeft (buttonRect.getWidth() / 2));
  960. decButton->setConnectedEdges (Button::ConnectedOnRight);
  961. incButton->setConnectedEdges (Button::ConnectedOnLeft);
  962. }
  963. else
  964. {
  965. decButton->setBounds (buttonRect.removeFromBottom (buttonRect.getHeight() / 2));
  966. decButton->setConnectedEdges (Button::ConnectedOnTop);
  967. incButton->setConnectedEdges (Button::ConnectedOnBottom);
  968. }
  969. incButton->setBounds (buttonRect);
  970. }
  971. //==============================================================================
  972. Slider& owner;
  973. SliderStyle style;
  974. ListenerList <SliderListener> listeners;
  975. Value currentValue, valueMin, valueMax;
  976. double lastCurrentValue, lastValueMin, lastValueMax;
  977. double minimum, maximum, interval, doubleClickReturnValue;
  978. double valueWhenLastDragged, valueOnMouseDown, skewFactor, lastAngle;
  979. double velocityModeSensitivity, velocityModeOffset, minMaxDiff;
  980. int velocityModeThreshold;
  981. RotaryParameters rotaryParams;
  982. Point<float> mouseDragStartPos, mousePosWhenLastDragged;
  983. int sliderRegionStart, sliderRegionSize;
  984. int sliderBeingDragged;
  985. int pixelsForFullDragExtent;
  986. Time lastMouseWheelTime;
  987. Rectangle<int> sliderRect;
  988. ScopedPointer<DragInProgress> currentDrag;
  989. TextEntryBoxPosition textBoxPos;
  990. String textSuffix;
  991. int numDecimalPlaces;
  992. int textBoxWidth, textBoxHeight;
  993. IncDecButtonMode incDecButtonMode;
  994. bool editableText;
  995. bool doubleClickToValue;
  996. bool isVelocityBased;
  997. bool userKeyOverridesVelocity;
  998. bool incDecButtonsSideBySide;
  999. bool sendChangeOnlyOnRelease;
  1000. bool popupDisplayEnabled;
  1001. bool menuEnabled;
  1002. bool useDragEvents;
  1003. bool incDecDragged;
  1004. bool scrollWheelEnabled;
  1005. bool snapsToMousePos;
  1006. ScopedPointer<Label> valueBox;
  1007. ScopedPointer<Button> incButton, decButton;
  1008. //==============================================================================
  1009. class PopupDisplayComponent : public BubbleComponent,
  1010. public Timer
  1011. {
  1012. public:
  1013. PopupDisplayComponent (Slider& s)
  1014. : owner (s),
  1015. font (s.getLookAndFeel().getSliderPopupFont (s))
  1016. {
  1017. setAlwaysOnTop (true);
  1018. setAllowedPlacement (owner.getLookAndFeel().getSliderPopupPlacement (s));
  1019. setLookAndFeel (&s.getLookAndFeel());
  1020. }
  1021. void paintContent (Graphics& g, int w, int h) override
  1022. {
  1023. g.setFont (font);
  1024. g.setColour (owner.findColour (TooltipWindow::textColourId, true));
  1025. g.drawFittedText (text, Rectangle<int> (w, h), Justification::centred, 1);
  1026. }
  1027. void getContentSize (int& w, int& h) override
  1028. {
  1029. w = font.getStringWidth (text) + 18;
  1030. h = (int) (font.getHeight() * 1.6f);
  1031. }
  1032. void updatePosition (const String& newText)
  1033. {
  1034. text = newText;
  1035. BubbleComponent::setPosition (&owner);
  1036. repaint();
  1037. }
  1038. void timerCallback() override
  1039. {
  1040. owner.pimpl->popupDisplay = nullptr;
  1041. }
  1042. private:
  1043. Slider& owner;
  1044. Font font;
  1045. String text;
  1046. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PopupDisplayComponent)
  1047. };
  1048. ScopedPointer<PopupDisplayComponent> popupDisplay;
  1049. Component* parentForPopupDisplay;
  1050. //==============================================================================
  1051. static double smallestAngleBetween (const double a1, const double a2) noexcept
  1052. {
  1053. return jmin (std::abs (a1 - a2),
  1054. std::abs (a1 + double_Pi * 2.0 - a2),
  1055. std::abs (a2 + double_Pi * 2.0 - a1));
  1056. }
  1057. };
  1058. //==============================================================================
  1059. Slider::Slider()
  1060. {
  1061. init (LinearHorizontal, TextBoxLeft);
  1062. }
  1063. Slider::Slider (const String& name) : Component (name)
  1064. {
  1065. init (LinearHorizontal, TextBoxLeft);
  1066. }
  1067. Slider::Slider (SliderStyle style, TextEntryBoxPosition textBoxPos)
  1068. {
  1069. init (style, textBoxPos);
  1070. }
  1071. void Slider::init (SliderStyle style, TextEntryBoxPosition textBoxPos)
  1072. {
  1073. setWantsKeyboardFocus (false);
  1074. setRepaintsOnMouseActivity (true);
  1075. pimpl = new Pimpl (*this, style, textBoxPos);
  1076. Slider::lookAndFeelChanged();
  1077. updateText();
  1078. pimpl->registerListeners();
  1079. }
  1080. Slider::~Slider() {}
  1081. //==============================================================================
  1082. void Slider::addListener (SliderListener* const listener) { pimpl->listeners.add (listener); }
  1083. void Slider::removeListener (SliderListener* const listener) { pimpl->listeners.remove (listener); }
  1084. //==============================================================================
  1085. Slider::SliderStyle Slider::getSliderStyle() const noexcept { return pimpl->style; }
  1086. void Slider::setSliderStyle (const SliderStyle newStyle) { pimpl->setSliderStyle (newStyle); }
  1087. void Slider::setRotaryParameters (RotaryParameters p) noexcept
  1088. {
  1089. // make sure the values are sensible..
  1090. jassert (p.startAngleRadians >= 0 && p.endAngleRadians >= 0);
  1091. jassert (p.startAngleRadians < float_Pi * 4.0f && p.endAngleRadians < float_Pi * 4.0f);
  1092. jassert (p.startAngleRadians < p.endAngleRadians);
  1093. pimpl->rotaryParams = p;
  1094. }
  1095. void Slider::setRotaryParameters (float startAngleRadians, float endAngleRadians, bool stopAtEnd) noexcept
  1096. {
  1097. RotaryParameters p = { startAngleRadians, endAngleRadians, stopAtEnd };
  1098. setRotaryParameters (p);
  1099. }
  1100. Slider::RotaryParameters Slider::getRotaryParameters() const noexcept
  1101. {
  1102. return pimpl->rotaryParams;
  1103. }
  1104. void Slider::setVelocityBasedMode (bool vb) { pimpl->isVelocityBased = vb; }
  1105. bool Slider::getVelocityBasedMode() const noexcept { return pimpl->isVelocityBased; }
  1106. bool Slider::getVelocityModeIsSwappable() const noexcept { return pimpl->userKeyOverridesVelocity; }
  1107. int Slider::getVelocityThreshold() const noexcept { return pimpl->velocityModeThreshold; }
  1108. double Slider::getVelocitySensitivity() const noexcept { return pimpl->velocityModeSensitivity; }
  1109. double Slider::getVelocityOffset() const noexcept { return pimpl->velocityModeOffset; }
  1110. void Slider::setVelocityModeParameters (const double sensitivity, const int threshold,
  1111. const double offset, const bool userCanPressKeyToSwapMode)
  1112. {
  1113. jassert (threshold >= 0);
  1114. jassert (sensitivity > 0);
  1115. jassert (offset >= 0);
  1116. pimpl->setVelocityModeParameters (sensitivity, threshold, offset, userCanPressKeyToSwapMode);
  1117. }
  1118. double Slider::getSkewFactor() const noexcept { return pimpl->skewFactor; }
  1119. void Slider::setSkewFactor (const double factor) { pimpl->skewFactor = factor; }
  1120. void Slider::setSkewFactorFromMidPoint (const double sliderValueToShowAtMidPoint)
  1121. {
  1122. pimpl->setSkewFactorFromMidPoint (sliderValueToShowAtMidPoint);
  1123. }
  1124. int Slider::getMouseDragSensitivity() const noexcept { return pimpl->pixelsForFullDragExtent; }
  1125. void Slider::setMouseDragSensitivity (const int distanceForFullScaleDrag)
  1126. {
  1127. jassert (distanceForFullScaleDrag > 0);
  1128. pimpl->pixelsForFullDragExtent = distanceForFullScaleDrag;
  1129. }
  1130. void Slider::setIncDecButtonsMode (const IncDecButtonMode mode) { pimpl->setIncDecButtonsMode (mode); }
  1131. Slider::TextEntryBoxPosition Slider::getTextBoxPosition() const noexcept { return pimpl->textBoxPos; }
  1132. int Slider::getTextBoxWidth() const noexcept { return pimpl->textBoxWidth; }
  1133. int Slider::getTextBoxHeight() const noexcept { return pimpl->textBoxHeight; }
  1134. void Slider::setTextBoxStyle (const TextEntryBoxPosition newPosition, const bool isReadOnly,
  1135. const int textEntryBoxWidth, const int textEntryBoxHeight)
  1136. {
  1137. pimpl->setTextBoxStyle (newPosition, isReadOnly, textEntryBoxWidth, textEntryBoxHeight);
  1138. }
  1139. bool Slider::isTextBoxEditable() const noexcept { return pimpl->editableText; }
  1140. void Slider::setTextBoxIsEditable (const bool shouldBeEditable) { pimpl->setTextBoxIsEditable (shouldBeEditable); }
  1141. void Slider::showTextBox() { pimpl->showTextBox(); }
  1142. void Slider::hideTextBox (const bool discardCurrentEditorContents) { pimpl->hideTextBox (discardCurrentEditorContents); }
  1143. void Slider::setChangeNotificationOnlyOnRelease (bool onlyNotifyOnRelease)
  1144. {
  1145. pimpl->sendChangeOnlyOnRelease = onlyNotifyOnRelease;
  1146. }
  1147. bool Slider::getSliderSnapsToMousePosition() const noexcept { return pimpl->snapsToMousePos; }
  1148. void Slider::setSliderSnapsToMousePosition (const bool shouldSnapToMouse) { pimpl->snapsToMousePos = shouldSnapToMouse; }
  1149. void Slider::setPopupDisplayEnabled (const bool enabled, Component* const parentComponentToUse)
  1150. {
  1151. pimpl->popupDisplayEnabled = enabled;
  1152. pimpl->parentForPopupDisplay = parentComponentToUse;
  1153. }
  1154. Component* Slider::getCurrentPopupDisplay() const noexcept { return pimpl->popupDisplay.get(); }
  1155. //==============================================================================
  1156. void Slider::colourChanged() { lookAndFeelChanged(); }
  1157. void Slider::lookAndFeelChanged() { pimpl->lookAndFeelChanged (getLookAndFeel()); }
  1158. void Slider::enablementChanged() { repaint(); pimpl->updateTextBoxEnablement(); }
  1159. //==============================================================================
  1160. double Slider::getMaximum() const noexcept { return pimpl->maximum; }
  1161. double Slider::getMinimum() const noexcept { return pimpl->minimum; }
  1162. double Slider::getInterval() const noexcept { return pimpl->interval; }
  1163. void Slider::setRange (double newMin, double newMax, double newInt)
  1164. {
  1165. pimpl->setRange (newMin, newMax, newInt);
  1166. }
  1167. Value& Slider::getValueObject() noexcept { return pimpl->currentValue; }
  1168. Value& Slider::getMinValueObject() noexcept { return pimpl->valueMin; }
  1169. Value& Slider::getMaxValueObject() noexcept { return pimpl->valueMax; }
  1170. double Slider::getValue() const { return pimpl->getValue(); }
  1171. void Slider::setValue (double newValue, const NotificationType notification)
  1172. {
  1173. pimpl->setValue (newValue, notification);
  1174. }
  1175. double Slider::getMinValue() const { return pimpl->getMinValue(); }
  1176. double Slider::getMaxValue() const { return pimpl->getMaxValue(); }
  1177. void Slider::setMinValue (double newValue, const NotificationType notification, bool allowNudgingOfOtherValues)
  1178. {
  1179. pimpl->setMinValue (newValue, notification, allowNudgingOfOtherValues);
  1180. }
  1181. void Slider::setMaxValue (double newValue, const NotificationType notification, bool allowNudgingOfOtherValues)
  1182. {
  1183. pimpl->setMaxValue (newValue, notification, allowNudgingOfOtherValues);
  1184. }
  1185. void Slider::setMinAndMaxValues (double newMinValue, double newMaxValue, const NotificationType notification)
  1186. {
  1187. pimpl->setMinAndMaxValues (newMinValue, newMaxValue, notification);
  1188. }
  1189. void Slider::setDoubleClickReturnValue (bool isDoubleClickEnabled, double valueToSetOnDoubleClick)
  1190. {
  1191. pimpl->doubleClickToValue = isDoubleClickEnabled;
  1192. pimpl->doubleClickReturnValue = valueToSetOnDoubleClick;
  1193. }
  1194. double Slider::getDoubleClickReturnValue() const noexcept { return pimpl->doubleClickReturnValue; }
  1195. bool Slider::isDoubleClickReturnEnabled() const noexcept { return pimpl->doubleClickToValue; }
  1196. void Slider::updateText()
  1197. {
  1198. pimpl->updateText();
  1199. }
  1200. void Slider::setTextValueSuffix (const String& suffix)
  1201. {
  1202. pimpl->setTextValueSuffix (suffix);
  1203. }
  1204. String Slider::getTextValueSuffix() const
  1205. {
  1206. return pimpl->textSuffix;
  1207. }
  1208. String Slider::getTextFromValue (double v)
  1209. {
  1210. if (getNumDecimalPlacesToDisplay() > 0)
  1211. return String (v, getNumDecimalPlacesToDisplay()) + getTextValueSuffix();
  1212. return String (roundToInt (v)) + getTextValueSuffix();
  1213. }
  1214. double Slider::getValueFromText (const String& text)
  1215. {
  1216. String t (text.trimStart());
  1217. if (t.endsWith (getTextValueSuffix()))
  1218. t = t.substring (0, t.length() - getTextValueSuffix().length());
  1219. while (t.startsWithChar ('+'))
  1220. t = t.substring (1).trimStart();
  1221. return t.initialSectionContainingOnly ("0123456789.,-")
  1222. .getDoubleValue();
  1223. }
  1224. double Slider::proportionOfLengthToValue (double proportion)
  1225. {
  1226. const double skew = getSkewFactor();
  1227. if (skew != 1.0 && proportion > 0.0)
  1228. proportion = exp (log (proportion) / skew);
  1229. return getMinimum() + (getMaximum() - getMinimum()) * proportion;
  1230. }
  1231. double Slider::valueToProportionOfLength (double value)
  1232. {
  1233. const double n = (value - getMinimum()) / (getMaximum() - getMinimum());
  1234. const double skew = getSkewFactor();
  1235. return skew == 1.0 ? n : pow (n, skew);
  1236. }
  1237. double Slider::snapValue (double attemptedValue, DragMode)
  1238. {
  1239. return attemptedValue;
  1240. }
  1241. int Slider::getNumDecimalPlacesToDisplay() const noexcept { return pimpl->numDecimalPlaces; }
  1242. //==============================================================================
  1243. int Slider::getThumbBeingDragged() const noexcept { return pimpl->sliderBeingDragged; }
  1244. void Slider::startedDragging() {}
  1245. void Slider::stoppedDragging() {}
  1246. void Slider::valueChanged() {}
  1247. //==============================================================================
  1248. void Slider::setPopupMenuEnabled (const bool menuEnabled) { pimpl->menuEnabled = menuEnabled; }
  1249. void Slider::setScrollWheelEnabled (const bool enabled) { pimpl->scrollWheelEnabled = enabled; }
  1250. bool Slider::isHorizontal() const noexcept { return pimpl->isHorizontal(); }
  1251. bool Slider::isVertical() const noexcept { return pimpl->isVertical(); }
  1252. bool Slider::isRotary() const noexcept { return pimpl->isRotary(); }
  1253. bool Slider::isBar() const noexcept { return pimpl->isBar(); }
  1254. float Slider::getPositionOfValue (const double value) const { return pimpl->getPositionOfValue (value); }
  1255. //==============================================================================
  1256. void Slider::paint (Graphics& g) { pimpl->paint (g, getLookAndFeel()); }
  1257. void Slider::resized() { pimpl->resized (getLookAndFeel()); }
  1258. void Slider::focusOfChildComponentChanged (FocusChangeType) { repaint(); }
  1259. void Slider::mouseDown (const MouseEvent& e) { pimpl->mouseDown (e); }
  1260. void Slider::mouseUp (const MouseEvent&) { pimpl->mouseUp(); }
  1261. void Slider::modifierKeysChanged (const ModifierKeys& modifiers)
  1262. {
  1263. if (isEnabled())
  1264. pimpl->modifierKeysChanged (modifiers);
  1265. }
  1266. void Slider::mouseDrag (const MouseEvent& e)
  1267. {
  1268. if (isEnabled())
  1269. pimpl->mouseDrag (e);
  1270. }
  1271. void Slider::mouseDoubleClick (const MouseEvent&)
  1272. {
  1273. if (isEnabled())
  1274. pimpl->mouseDoubleClick();
  1275. }
  1276. void Slider::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel)
  1277. {
  1278. if (! (isEnabled() && pimpl->mouseWheelMove (e, wheel)))
  1279. Component::mouseWheelMove (e, wheel);
  1280. }