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.

1672 lines
61KB

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