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.

1620 lines
59KB

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