The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1641 lines
59KB

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