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.

1655 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. if (popupDisplay != nullptr)
  694. popupDisplay->stopTimer();
  695. }
  696. currentDrag = new DragInProgress (*this);
  697. mouseDrag (e);
  698. }
  699. }
  700. }
  701. void mouseDrag (const MouseEvent& e)
  702. {
  703. if (useDragEvents && maximum > minimum
  704. && ! ((style == LinearBar || style == LinearBarVertical)
  705. && e.mouseWasClicked() && valueBox != nullptr && valueBox->isEditable()))
  706. {
  707. DragMode dragMode = notDragging;
  708. if (style == Rotary)
  709. {
  710. handleRotaryDrag (e);
  711. }
  712. else
  713. {
  714. if (style == IncDecButtons && ! incDecDragged)
  715. {
  716. if (e.getDistanceFromDragStart() < 10 || ! e.mouseWasDraggedSinceMouseDown())
  717. return;
  718. incDecDragged = true;
  719. mouseDragStartPos = e.position;
  720. }
  721. if (isAbsoluteDragMode (e.mods) || (maximum - minimum) / sliderRegionSize < interval)
  722. {
  723. dragMode = absoluteDrag;
  724. handleAbsoluteDrag (e);
  725. }
  726. else
  727. {
  728. dragMode = velocityDrag;
  729. handleVelocityDrag (e);
  730. }
  731. }
  732. valueWhenLastDragged = jlimit (minimum, maximum, valueWhenLastDragged);
  733. if (sliderBeingDragged == 0)
  734. {
  735. setValue (owner.snapValue (valueWhenLastDragged, dragMode),
  736. sendChangeOnlyOnRelease ? dontSendNotification : sendNotificationSync);
  737. }
  738. else if (sliderBeingDragged == 1)
  739. {
  740. setMinValue (owner.snapValue (valueWhenLastDragged, dragMode),
  741. sendChangeOnlyOnRelease ? dontSendNotification : sendNotificationAsync, true);
  742. if (e.mods.isShiftDown())
  743. setMaxValue (getMinValue() + minMaxDiff, dontSendNotification, true);
  744. else
  745. minMaxDiff = static_cast<double> (valueMax.getValue()) - static_cast<double> (valueMin.getValue());
  746. }
  747. else if (sliderBeingDragged == 2)
  748. {
  749. setMaxValue (owner.snapValue (valueWhenLastDragged, dragMode),
  750. sendChangeOnlyOnRelease ? dontSendNotification : sendNotificationAsync, true);
  751. if (e.mods.isShiftDown())
  752. setMinValue (getMaxValue() - minMaxDiff, dontSendNotification, true);
  753. else
  754. minMaxDiff = static_cast<double> (valueMax.getValue()) - static_cast<double> (valueMin.getValue());
  755. }
  756. mousePosWhenLastDragged = e.position;
  757. }
  758. }
  759. void mouseUp()
  760. {
  761. if (owner.isEnabled()
  762. && useDragEvents
  763. && (maximum > minimum)
  764. && (style != IncDecButtons || incDecDragged))
  765. {
  766. restoreMouseIfHidden();
  767. if (sendChangeOnlyOnRelease && valueOnMouseDown != static_cast<double> (currentValue.getValue()))
  768. triggerChangeMessage (sendNotificationAsync);
  769. currentDrag = nullptr;
  770. popupDisplay = nullptr;
  771. if (style == IncDecButtons)
  772. {
  773. incButton->setState (Button::buttonNormal);
  774. decButton->setState (Button::buttonNormal);
  775. }
  776. }
  777. else if (popupDisplay != nullptr)
  778. {
  779. popupDisplay->startTimer (200);
  780. }
  781. currentDrag = nullptr;
  782. }
  783. void mouseMove()
  784. {
  785. auto isTwoValue = (style == TwoValueHorizontal || style == TwoValueVertical);
  786. auto isThreeValue = (style == ThreeValueHorizontal || style == ThreeValueVertical);
  787. // this is a workaround for a bug where the popup display being dismissed triggers
  788. // a mouse move causing it to never be hidden
  789. auto shouldShowPopup = showPopupOnHover
  790. && (Time::getMillisecondCounterHiRes() - lastPopupDismissal) > 250;
  791. if (shouldShowPopup
  792. && ! isTwoValue
  793. && ! isThreeValue)
  794. {
  795. if (owner.isMouseOver (true))
  796. {
  797. if (popupDisplay == nullptr)
  798. showPopupDisplay();
  799. if (popupDisplay != nullptr && popupHoverTimeout != -1)
  800. popupDisplay->startTimer (popupHoverTimeout);
  801. }
  802. }
  803. }
  804. void mouseExit()
  805. {
  806. popupDisplay = nullptr;
  807. }
  808. void showPopupDisplay()
  809. {
  810. if (style == IncDecButtons)
  811. return;
  812. if (popupDisplay == nullptr)
  813. {
  814. popupDisplay = new PopupDisplayComponent (owner);
  815. if (parentForPopupDisplay != nullptr)
  816. parentForPopupDisplay->addChildComponent (popupDisplay);
  817. else
  818. popupDisplay->addToDesktop (ComponentPeer::windowIsTemporary);
  819. if (style == SliderStyle::TwoValueHorizontal
  820. || style == SliderStyle::TwoValueVertical)
  821. {
  822. updatePopupDisplay (sliderBeingDragged == 2 ? getMaxValue()
  823. : getMinValue());
  824. }
  825. else
  826. {
  827. updatePopupDisplay (getValue());
  828. }
  829. popupDisplay->setVisible (true);
  830. }
  831. }
  832. void updatePopupDisplay (double valueToShow)
  833. {
  834. if (popupDisplay != nullptr)
  835. popupDisplay->updatePosition (owner.getTextFromValue (valueToShow));
  836. }
  837. bool canDoubleClickToValue() const
  838. {
  839. return doubleClickToValue
  840. && style != IncDecButtons
  841. && minimum <= doubleClickReturnValue
  842. && maximum >= doubleClickReturnValue;
  843. }
  844. void mouseDoubleClick()
  845. {
  846. if (canDoubleClickToValue())
  847. {
  848. DragInProgress drag (*this);
  849. setValue (doubleClickReturnValue, sendNotificationSync);
  850. }
  851. }
  852. double getMouseWheelDelta (double value, double wheelAmount)
  853. {
  854. if (style == IncDecButtons)
  855. return interval * wheelAmount;
  856. auto proportionDelta = wheelAmount * 0.15;
  857. auto currentPos = owner.valueToProportionOfLength (value);
  858. return owner.proportionOfLengthToValue (jlimit (0.0, 1.0, currentPos + proportionDelta)) - value;
  859. }
  860. bool mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel)
  861. {
  862. if (scrollWheelEnabled
  863. && style != TwoValueHorizontal
  864. && style != TwoValueVertical)
  865. {
  866. // sometimes duplicate wheel events seem to be sent, so since we're going to
  867. // bump the value by a minimum of the interval, avoid doing this twice..
  868. if (e.eventTime != lastMouseWheelTime)
  869. {
  870. lastMouseWheelTime = e.eventTime;
  871. if (maximum > minimum && ! e.mods.isAnyMouseButtonDown())
  872. {
  873. if (valueBox != nullptr)
  874. valueBox->hideEditor (false);
  875. auto value = static_cast<double> (currentValue.getValue());
  876. auto delta = getMouseWheelDelta (value, (std::abs (wheel.deltaX) > std::abs (wheel.deltaY)
  877. ? -wheel.deltaX : wheel.deltaY)
  878. * (wheel.isReversed ? -1.0f : 1.0f));
  879. if (delta != 0.0)
  880. {
  881. auto newValue = value + jmax (interval, std::abs (delta)) * (delta < 0 ? -1.0 : 1.0);
  882. DragInProgress drag (*this);
  883. setValue (owner.snapValue (newValue, notDragging), sendNotificationSync);
  884. }
  885. }
  886. }
  887. return true;
  888. }
  889. return false;
  890. }
  891. void modifierKeysChanged (const ModifierKeys& modifiers)
  892. {
  893. if (style != IncDecButtons && style != Rotary && isAbsoluteDragMode (modifiers))
  894. restoreMouseIfHidden();
  895. }
  896. bool isAbsoluteDragMode (ModifierKeys mods) const
  897. {
  898. return isVelocityBased == (userKeyOverridesVelocity
  899. && mods.testFlags (ModifierKeys::ctrlAltCommandModifiers));
  900. }
  901. void restoreMouseIfHidden()
  902. {
  903. for (auto& ms : Desktop::getInstance().getMouseSources())
  904. {
  905. if (ms.isUnboundedMouseMovementEnabled())
  906. {
  907. ms.enableUnboundedMouseMovement (false);
  908. auto pos = sliderBeingDragged == 2 ? getMaxValue()
  909. : (sliderBeingDragged == 1 ? getMinValue()
  910. : static_cast<double> (currentValue.getValue()));
  911. Point<float> mousePos;
  912. if (isRotary())
  913. {
  914. mousePos = ms.getLastMouseDownPosition();
  915. auto delta = (float) (pixelsForFullDragExtent * (owner.valueToProportionOfLength (valueOnMouseDown)
  916. - owner.valueToProportionOfLength (pos)));
  917. if (style == RotaryHorizontalDrag) mousePos += Point<float> (-delta, 0.0f);
  918. else if (style == RotaryVerticalDrag) mousePos += Point<float> (0.0f, delta);
  919. else mousePos += Point<float> (delta / -2.0f, delta / 2.0f);
  920. mousePos = owner.getScreenBounds().reduced (4).toFloat().getConstrainedPoint (mousePos);
  921. mouseDragStartPos = mousePosWhenLastDragged = owner.getLocalPoint (nullptr, mousePos);
  922. valueOnMouseDown = valueWhenLastDragged;
  923. }
  924. else
  925. {
  926. auto pixelPos = (float) getLinearSliderPos (pos);
  927. mousePos = owner.localPointToGlobal (Point<float> (isHorizontal() ? pixelPos : (owner.getWidth() / 2.0f),
  928. isVertical() ? pixelPos : (owner.getHeight() / 2.0f)));
  929. }
  930. ms.setScreenPosition (mousePos);
  931. }
  932. }
  933. }
  934. //==============================================================================
  935. void paint (Graphics& g, LookAndFeel& lf)
  936. {
  937. if (style != IncDecButtons)
  938. {
  939. if (isRotary())
  940. {
  941. auto sliderPos = (float) owner.valueToProportionOfLength (lastCurrentValue);
  942. jassert (sliderPos >= 0 && sliderPos <= 1.0f);
  943. lf.drawRotarySlider (g,
  944. sliderRect.getX(), sliderRect.getY(),
  945. sliderRect.getWidth(), sliderRect.getHeight(),
  946. sliderPos, rotaryParams.startAngleRadians,
  947. rotaryParams.endAngleRadians, owner);
  948. }
  949. else
  950. {
  951. lf.drawLinearSlider (g,
  952. sliderRect.getX(), sliderRect.getY(),
  953. sliderRect.getWidth(), sliderRect.getHeight(),
  954. getLinearSliderPos (lastCurrentValue),
  955. getLinearSliderPos (lastValueMin),
  956. getLinearSliderPos (lastValueMax),
  957. style, owner);
  958. }
  959. if ((style == LinearBar || style == LinearBarVertical) && valueBox == nullptr)
  960. {
  961. g.setColour (owner.findColour (Slider::textBoxOutlineColourId));
  962. g.drawRect (0, 0, owner.getWidth(), owner.getHeight(), 1);
  963. }
  964. }
  965. }
  966. //==============================================================================
  967. void resized (LookAndFeel& lf)
  968. {
  969. auto layout = lf.getSliderLayout (owner);
  970. sliderRect = layout.sliderBounds;
  971. if (valueBox != nullptr)
  972. valueBox->setBounds (layout.textBoxBounds);
  973. if (isHorizontal())
  974. {
  975. sliderRegionStart = layout.sliderBounds.getX();
  976. sliderRegionSize = layout.sliderBounds.getWidth();
  977. }
  978. else if (isVertical())
  979. {
  980. sliderRegionStart = layout.sliderBounds.getY();
  981. sliderRegionSize = layout.sliderBounds.getHeight();
  982. }
  983. else if (style == IncDecButtons)
  984. {
  985. resizeIncDecButtons();
  986. }
  987. }
  988. //==============================================================================
  989. void resizeIncDecButtons()
  990. {
  991. auto buttonRect = sliderRect;
  992. if (textBoxPos == TextBoxLeft || textBoxPos == TextBoxRight)
  993. buttonRect.expand (-2, 0);
  994. else
  995. buttonRect.expand (0, -2);
  996. incDecButtonsSideBySide = buttonRect.getWidth() > buttonRect.getHeight();
  997. if (incDecButtonsSideBySide)
  998. {
  999. decButton->setBounds (buttonRect.removeFromLeft (buttonRect.getWidth() / 2));
  1000. decButton->setConnectedEdges (Button::ConnectedOnRight);
  1001. incButton->setConnectedEdges (Button::ConnectedOnLeft);
  1002. }
  1003. else
  1004. {
  1005. decButton->setBounds (buttonRect.removeFromBottom (buttonRect.getHeight() / 2));
  1006. decButton->setConnectedEdges (Button::ConnectedOnTop);
  1007. incButton->setConnectedEdges (Button::ConnectedOnBottom);
  1008. }
  1009. incButton->setBounds (buttonRect);
  1010. }
  1011. //==============================================================================
  1012. Slider& owner;
  1013. SliderStyle style;
  1014. ListenerList<Slider::Listener> listeners;
  1015. Value currentValue, valueMin, valueMax;
  1016. double lastCurrentValue = 0, lastValueMin = 0, lastValueMax = 0;
  1017. double minimum = 0, maximum = 10, interval = 0, doubleClickReturnValue = 0;
  1018. double valueWhenLastDragged = 0, valueOnMouseDown = 0, skewFactor = 1.0, lastAngle = 0;
  1019. bool symmetricSkew = false;
  1020. double velocityModeSensitivity = 1.0, velocityModeOffset = 0, minMaxDiff = 0;
  1021. int velocityModeThreshold = 1;
  1022. RotaryParameters rotaryParams;
  1023. Point<float> mouseDragStartPos, mousePosWhenLastDragged;
  1024. int sliderRegionStart = 0, sliderRegionSize = 1;
  1025. int sliderBeingDragged = -1;
  1026. int pixelsForFullDragExtent = 250;
  1027. Time lastMouseWheelTime;
  1028. Rectangle<int> sliderRect;
  1029. ScopedPointer<DragInProgress> currentDrag;
  1030. TextEntryBoxPosition textBoxPos;
  1031. String textSuffix;
  1032. int numDecimalPlaces = 7;
  1033. int textBoxWidth = 80, textBoxHeight = 20;
  1034. IncDecButtonMode incDecButtonMode = incDecButtonsNotDraggable;
  1035. bool editableText = true;
  1036. bool doubleClickToValue = false;
  1037. bool isVelocityBased = false;
  1038. bool userKeyOverridesVelocity = true;
  1039. bool incDecButtonsSideBySide = false;
  1040. bool sendChangeOnlyOnRelease = false;
  1041. bool showPopupOnDrag = false;
  1042. bool showPopupOnHover = false;
  1043. bool menuEnabled = false;
  1044. bool useDragEvents = false;
  1045. bool incDecDragged = false;
  1046. bool scrollWheelEnabled = true;
  1047. bool snapsToMousePos = true;
  1048. int popupHoverTimeout = 2000;
  1049. double lastPopupDismissal = 0.0;
  1050. ScopedPointer<Label> valueBox;
  1051. ScopedPointer<Button> incButton, decButton;
  1052. //==============================================================================
  1053. struct PopupDisplayComponent : public BubbleComponent,
  1054. public Timer
  1055. {
  1056. PopupDisplayComponent (Slider& s)
  1057. : owner (s),
  1058. font (s.getLookAndFeel().getSliderPopupFont (s))
  1059. {
  1060. setAlwaysOnTop (true);
  1061. setAllowedPlacement (owner.getLookAndFeel().getSliderPopupPlacement (s));
  1062. setLookAndFeel (&s.getLookAndFeel());
  1063. }
  1064. ~PopupDisplayComponent()
  1065. {
  1066. owner.pimpl->lastPopupDismissal = Time::getMillisecondCounterHiRes();
  1067. }
  1068. void paintContent (Graphics& g, int w, int h) override
  1069. {
  1070. g.setFont (font);
  1071. g.setColour (owner.findColour (TooltipWindow::textColourId, true));
  1072. g.drawFittedText (text, Rectangle<int> (w, h), Justification::centred, 1);
  1073. }
  1074. void getContentSize (int& w, int& h) override
  1075. {
  1076. w = font.getStringWidth (text) + 18;
  1077. h = (int) (font.getHeight() * 1.6f);
  1078. }
  1079. void updatePosition (const String& newText)
  1080. {
  1081. text = newText;
  1082. BubbleComponent::setPosition (&owner);
  1083. repaint();
  1084. }
  1085. void timerCallback() override
  1086. {
  1087. stopTimer();
  1088. owner.pimpl->popupDisplay = nullptr;
  1089. }
  1090. private:
  1091. Slider& owner;
  1092. Font font;
  1093. String text;
  1094. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PopupDisplayComponent)
  1095. };
  1096. ScopedPointer<PopupDisplayComponent> popupDisplay;
  1097. Component* parentForPopupDisplay = nullptr;
  1098. //==============================================================================
  1099. static double smallestAngleBetween (double a1, double a2) noexcept
  1100. {
  1101. return jmin (std::abs (a1 - a2),
  1102. std::abs (a1 + double_Pi * 2.0 - a2),
  1103. std::abs (a2 + double_Pi * 2.0 - a1));
  1104. }
  1105. };
  1106. //==============================================================================
  1107. Slider::Slider()
  1108. {
  1109. init (LinearHorizontal, TextBoxLeft);
  1110. }
  1111. Slider::Slider (const String& name) : Component (name)
  1112. {
  1113. init (LinearHorizontal, TextBoxLeft);
  1114. }
  1115. Slider::Slider (SliderStyle style, TextEntryBoxPosition textBoxPos)
  1116. {
  1117. init (style, textBoxPos);
  1118. }
  1119. void Slider::init (SliderStyle style, TextEntryBoxPosition textBoxPos)
  1120. {
  1121. setWantsKeyboardFocus (false);
  1122. setRepaintsOnMouseActivity (true);
  1123. pimpl = new Pimpl (*this, style, textBoxPos);
  1124. Slider::lookAndFeelChanged();
  1125. updateText();
  1126. pimpl->registerListeners();
  1127. }
  1128. Slider::~Slider() {}
  1129. //==============================================================================
  1130. void Slider::addListener (Listener* l) { pimpl->listeners.add (l); }
  1131. void Slider::removeListener (Listener* l) { pimpl->listeners.remove (l); }
  1132. //==============================================================================
  1133. Slider::SliderStyle Slider::getSliderStyle() const noexcept { return pimpl->style; }
  1134. void Slider::setSliderStyle (SliderStyle newStyle) { pimpl->setSliderStyle (newStyle); }
  1135. void Slider::setRotaryParameters (RotaryParameters p) noexcept
  1136. {
  1137. // make sure the values are sensible..
  1138. jassert (p.startAngleRadians >= 0 && p.endAngleRadians >= 0);
  1139. jassert (p.startAngleRadians < float_Pi * 4.0f && p.endAngleRadians < float_Pi * 4.0f);
  1140. pimpl->rotaryParams = p;
  1141. }
  1142. void Slider::setRotaryParameters (float startAngleRadians, float endAngleRadians, bool stopAtEnd) noexcept
  1143. {
  1144. RotaryParameters p = { startAngleRadians, endAngleRadians, stopAtEnd };
  1145. setRotaryParameters (p);
  1146. }
  1147. Slider::RotaryParameters Slider::getRotaryParameters() const noexcept
  1148. {
  1149. return pimpl->rotaryParams;
  1150. }
  1151. void Slider::setVelocityBasedMode (bool vb) { pimpl->isVelocityBased = vb; }
  1152. bool Slider::getVelocityBasedMode() const noexcept { return pimpl->isVelocityBased; }
  1153. bool Slider::getVelocityModeIsSwappable() const noexcept { return pimpl->userKeyOverridesVelocity; }
  1154. int Slider::getVelocityThreshold() const noexcept { return pimpl->velocityModeThreshold; }
  1155. double Slider::getVelocitySensitivity() const noexcept { return pimpl->velocityModeSensitivity; }
  1156. double Slider::getVelocityOffset() const noexcept { return pimpl->velocityModeOffset; }
  1157. void Slider::setVelocityModeParameters (double sensitivity, int threshold, double offset, bool userCanPressKeyToSwapMode)
  1158. {
  1159. jassert (threshold >= 0);
  1160. jassert (sensitivity > 0);
  1161. jassert (offset >= 0);
  1162. pimpl->setVelocityModeParameters (sensitivity, threshold, offset, userCanPressKeyToSwapMode);
  1163. }
  1164. double Slider::getSkewFactor() const noexcept { return pimpl->skewFactor; }
  1165. bool Slider::isSymmetricSkew() const noexcept { return pimpl->symmetricSkew; }
  1166. void Slider::setSkewFactor (double factor, bool symmetricSkew)
  1167. {
  1168. pimpl->skewFactor = factor;
  1169. pimpl->symmetricSkew = symmetricSkew;
  1170. }
  1171. void Slider::setSkewFactorFromMidPoint (double sliderValueToShowAtMidPoint)
  1172. {
  1173. pimpl->setSkewFactorFromMidPoint (sliderValueToShowAtMidPoint);
  1174. pimpl->symmetricSkew = false;
  1175. }
  1176. int Slider::getMouseDragSensitivity() const noexcept { return pimpl->pixelsForFullDragExtent; }
  1177. void Slider::setMouseDragSensitivity (int distanceForFullScaleDrag)
  1178. {
  1179. jassert (distanceForFullScaleDrag > 0);
  1180. pimpl->pixelsForFullDragExtent = distanceForFullScaleDrag;
  1181. }
  1182. void Slider::setIncDecButtonsMode (IncDecButtonMode mode) { pimpl->setIncDecButtonsMode (mode); }
  1183. Slider::TextEntryBoxPosition Slider::getTextBoxPosition() const noexcept { return pimpl->textBoxPos; }
  1184. int Slider::getTextBoxWidth() const noexcept { return pimpl->textBoxWidth; }
  1185. int Slider::getTextBoxHeight() const noexcept { return pimpl->textBoxHeight; }
  1186. void Slider::setTextBoxStyle (TextEntryBoxPosition newPosition, bool isReadOnly, int textEntryBoxWidth, int textEntryBoxHeight)
  1187. {
  1188. pimpl->setTextBoxStyle (newPosition, isReadOnly, textEntryBoxWidth, textEntryBoxHeight);
  1189. }
  1190. bool Slider::isTextBoxEditable() const noexcept { return pimpl->editableText; }
  1191. void Slider::setTextBoxIsEditable (const bool shouldBeEditable) { pimpl->setTextBoxIsEditable (shouldBeEditable); }
  1192. void Slider::showTextBox() { pimpl->showTextBox(); }
  1193. void Slider::hideTextBox (bool discardCurrentEditorContents) { pimpl->hideTextBox (discardCurrentEditorContents); }
  1194. void Slider::setChangeNotificationOnlyOnRelease (bool onlyNotifyOnRelease)
  1195. {
  1196. pimpl->sendChangeOnlyOnRelease = onlyNotifyOnRelease;
  1197. }
  1198. bool Slider::getSliderSnapsToMousePosition() const noexcept { return pimpl->snapsToMousePos; }
  1199. void Slider::setSliderSnapsToMousePosition (bool shouldSnapToMouse) { pimpl->snapsToMousePos = shouldSnapToMouse; }
  1200. void Slider::setPopupDisplayEnabled (bool showOnDrag, bool showOnHover, Component* parent, int hoverTimeout)
  1201. {
  1202. pimpl->showPopupOnDrag = showOnDrag;
  1203. pimpl->showPopupOnHover = showOnHover;
  1204. pimpl->parentForPopupDisplay = parent;
  1205. pimpl->popupHoverTimeout = hoverTimeout;
  1206. }
  1207. Component* Slider::getCurrentPopupDisplay() const noexcept { return pimpl->popupDisplay.get(); }
  1208. //==============================================================================
  1209. void Slider::colourChanged() { lookAndFeelChanged(); }
  1210. void Slider::lookAndFeelChanged() { pimpl->lookAndFeelChanged (getLookAndFeel()); }
  1211. void Slider::enablementChanged() { repaint(); pimpl->updateTextBoxEnablement(); }
  1212. //==============================================================================
  1213. Range<double> Slider::getRange() const noexcept { return { pimpl->minimum, pimpl->maximum }; }
  1214. double Slider::getMaximum() const noexcept { return pimpl->maximum; }
  1215. double Slider::getMinimum() const noexcept { return pimpl->minimum; }
  1216. double Slider::getInterval() const noexcept { return pimpl->interval; }
  1217. void Slider::setRange (double newMin, double newMax, double newInt) { pimpl->setRange (newMin, newMax, newInt); }
  1218. void Slider::setRange (Range<double> newRange, double newInt) { pimpl->setRange (newRange.getStart(), newRange.getEnd(), newInt); }
  1219. double Slider::getValue() const { return pimpl->getValue(); }
  1220. Value& Slider::getValueObject() noexcept { return pimpl->currentValue; }
  1221. Value& Slider::getMinValueObject() noexcept { return pimpl->valueMin; }
  1222. Value& Slider::getMaxValueObject() noexcept { return pimpl->valueMax; }
  1223. void Slider::setValue (double newValue, NotificationType notification)
  1224. {
  1225. pimpl->setValue (newValue, notification);
  1226. }
  1227. double Slider::getMinValue() const { return pimpl->getMinValue(); }
  1228. double Slider::getMaxValue() const { return pimpl->getMaxValue(); }
  1229. void Slider::setMinValue (double newValue, NotificationType notification, bool allowNudgingOfOtherValues)
  1230. {
  1231. pimpl->setMinValue (newValue, notification, allowNudgingOfOtherValues);
  1232. }
  1233. void Slider::setMaxValue (double newValue, NotificationType notification, bool allowNudgingOfOtherValues)
  1234. {
  1235. pimpl->setMaxValue (newValue, notification, allowNudgingOfOtherValues);
  1236. }
  1237. void Slider::setMinAndMaxValues (double newMinValue, double newMaxValue, NotificationType notification)
  1238. {
  1239. pimpl->setMinAndMaxValues (newMinValue, newMaxValue, notification);
  1240. }
  1241. void Slider::setDoubleClickReturnValue (bool isDoubleClickEnabled, double valueToSetOnDoubleClick)
  1242. {
  1243. pimpl->doubleClickToValue = isDoubleClickEnabled;
  1244. pimpl->doubleClickReturnValue = valueToSetOnDoubleClick;
  1245. }
  1246. double Slider::getDoubleClickReturnValue() const noexcept { return pimpl->doubleClickReturnValue; }
  1247. bool Slider::isDoubleClickReturnEnabled() const noexcept { return pimpl->doubleClickToValue; }
  1248. void Slider::updateText()
  1249. {
  1250. pimpl->updateText();
  1251. }
  1252. void Slider::setTextValueSuffix (const String& suffix)
  1253. {
  1254. pimpl->setTextValueSuffix (suffix);
  1255. }
  1256. String Slider::getTextValueSuffix() const
  1257. {
  1258. return pimpl->textSuffix;
  1259. }
  1260. String Slider::getTextFromValue (double v)
  1261. {
  1262. if (getNumDecimalPlacesToDisplay() > 0)
  1263. return String (v, getNumDecimalPlacesToDisplay()) + getTextValueSuffix();
  1264. return String (roundToInt (v)) + getTextValueSuffix();
  1265. }
  1266. double Slider::getValueFromText (const String& text)
  1267. {
  1268. String t (text.trimStart());
  1269. if (t.endsWith (getTextValueSuffix()))
  1270. t = t.substring (0, t.length() - getTextValueSuffix().length());
  1271. while (t.startsWithChar ('+'))
  1272. t = t.substring (1).trimStart();
  1273. return t.initialSectionContainingOnly ("0123456789.,-")
  1274. .getDoubleValue();
  1275. }
  1276. double Slider::proportionOfLengthToValue (double proportion)
  1277. {
  1278. auto skew = getSkewFactor();
  1279. if (! isSymmetricSkew())
  1280. {
  1281. if (skew != 1.0 && proportion > 0.0)
  1282. proportion = std::exp (std::log (proportion) / skew);
  1283. return getMinimum() + (getMaximum() - getMinimum()) * proportion;
  1284. }
  1285. double distanceFromMiddle = 2.0 * proportion - 1.0;
  1286. if (skew != 1.0 && distanceFromMiddle != 0.0)
  1287. distanceFromMiddle = std::exp (std::log (std::abs (distanceFromMiddle)) / skew)
  1288. * (distanceFromMiddle < 0 ? -1 : 1);
  1289. return getMinimum() + (getMaximum() - getMinimum()) / 2.0 * (1 + distanceFromMiddle);
  1290. }
  1291. double Slider::valueToProportionOfLength (double value)
  1292. {
  1293. auto n = (value - getMinimum()) / (getMaximum() - getMinimum());
  1294. auto skew = getSkewFactor();
  1295. if (skew == 1.0)
  1296. return n;
  1297. if (! isSymmetricSkew())
  1298. return std::pow (n, skew);
  1299. double distanceFromMiddle = 2.0 * n - 1.0;
  1300. return (1.0 + std::pow (std::abs (distanceFromMiddle), skew) * (distanceFromMiddle < 0 ? -1 : 1)) / 2.0;
  1301. }
  1302. double Slider::snapValue (double attemptedValue, DragMode)
  1303. {
  1304. return attemptedValue;
  1305. }
  1306. int Slider::getNumDecimalPlacesToDisplay() const noexcept { return pimpl->numDecimalPlaces; }
  1307. //==============================================================================
  1308. int Slider::getThumbBeingDragged() const noexcept { return pimpl->sliderBeingDragged; }
  1309. void Slider::startedDragging() {}
  1310. void Slider::stoppedDragging() {}
  1311. void Slider::valueChanged() {}
  1312. //==============================================================================
  1313. void Slider::setPopupMenuEnabled (bool menuEnabled) { pimpl->menuEnabled = menuEnabled; }
  1314. void Slider::setScrollWheelEnabled (bool enabled) { pimpl->scrollWheelEnabled = enabled; }
  1315. bool Slider::isHorizontal() const noexcept { return pimpl->isHorizontal(); }
  1316. bool Slider::isVertical() const noexcept { return pimpl->isVertical(); }
  1317. bool Slider::isRotary() const noexcept { return pimpl->isRotary(); }
  1318. bool Slider::isBar() const noexcept { return pimpl->isBar(); }
  1319. float Slider::getPositionOfValue (double value) const { return pimpl->getPositionOfValue (value); }
  1320. //==============================================================================
  1321. void Slider::paint (Graphics& g) { pimpl->paint (g, getLookAndFeel()); }
  1322. void Slider::resized() { pimpl->resized (getLookAndFeel()); }
  1323. void Slider::focusOfChildComponentChanged (FocusChangeType) { repaint(); }
  1324. void Slider::mouseDown (const MouseEvent& e) { pimpl->mouseDown (e); }
  1325. void Slider::mouseUp (const MouseEvent&) { pimpl->mouseUp(); }
  1326. void Slider::mouseMove (const MouseEvent&) { pimpl->mouseMove(); }
  1327. void Slider::mouseExit (const MouseEvent&) { pimpl->mouseExit(); }
  1328. // If popup display is enabled and set to show on mouse hover, this makes sure
  1329. // it is shown when dragging the mouse over a slider and releasing
  1330. void Slider::mouseEnter (const MouseEvent&) { pimpl->mouseMove(); }
  1331. void Slider::modifierKeysChanged (const ModifierKeys& modifiers)
  1332. {
  1333. if (isEnabled())
  1334. pimpl->modifierKeysChanged (modifiers);
  1335. }
  1336. void Slider::mouseDrag (const MouseEvent& e)
  1337. {
  1338. if (isEnabled())
  1339. pimpl->mouseDrag (e);
  1340. }
  1341. void Slider::mouseDoubleClick (const MouseEvent&)
  1342. {
  1343. if (isEnabled())
  1344. pimpl->mouseDoubleClick();
  1345. }
  1346. void Slider::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel)
  1347. {
  1348. if (! (isEnabled() && pimpl->mouseWheelMove (e, wheel)))
  1349. Component::mouseWheelMove (e, wheel);
  1350. }
  1351. } // namespace juce