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.

1648 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 Value::Listener
  24. {
  25. public:
  26. Pimpl (Slider& s, SliderStyle sliderStyle, TextEntryBoxPosition textBoxPosition)
  27. : owner (s),
  28. style (sliderStyle),
  29. textBoxPos (textBoxPosition)
  30. {
  31. rotaryParams.startAngleRadians = MathConstants<float>::pi * 1.2f;
  32. rotaryParams.endAngleRadians = MathConstants<float>::pi * 2.8f;
  33. rotaryParams.stopAtEnd = true;
  34. }
  35. ~Pimpl()
  36. {
  37. currentValue.removeListener (this);
  38. valueMin.removeListener (this);
  39. valueMax.removeListener (this);
  40. popupDisplay.reset();
  41. }
  42. //==============================================================================
  43. void registerListeners()
  44. {
  45. currentValue.addListener (this);
  46. valueMin.addListener (this);
  47. valueMax.addListener (this);
  48. }
  49. bool isHorizontal() const noexcept
  50. {
  51. return style == LinearHorizontal
  52. || style == LinearBar
  53. || style == TwoValueHorizontal
  54. || style == ThreeValueHorizontal;
  55. }
  56. bool isVertical() const noexcept
  57. {
  58. return style == LinearVertical
  59. || style == LinearBarVertical
  60. || style == TwoValueVertical
  61. || style == ThreeValueVertical;
  62. }
  63. bool isRotary() const noexcept
  64. {
  65. return style == Rotary
  66. || style == RotaryHorizontalDrag
  67. || style == RotaryVerticalDrag
  68. || style == RotaryHorizontalVerticalDrag;
  69. }
  70. bool isBar() const noexcept
  71. {
  72. return style == LinearBar
  73. || style == LinearBarVertical;
  74. }
  75. bool incDecDragDirectionIsHorizontal() const noexcept
  76. {
  77. return incDecButtonMode == incDecButtonsDraggable_Horizontal
  78. || (incDecButtonMode == incDecButtonsDraggable_AutoDirection && incDecButtonsSideBySide);
  79. }
  80. float getPositionOfValue (double value) const
  81. {
  82. if (isHorizontal() || isVertical())
  83. return getLinearSliderPos (value);
  84. jassertfalse; // not a valid call on a slider that doesn't work linearly!
  85. return 0.0f;
  86. }
  87. void updateRange()
  88. {
  89. // figure out the number of DPs needed to display all values at this
  90. // interval setting.
  91. numDecimalPlaces = 7;
  92. if (normRange.interval != 0.0)
  93. {
  94. int v = std::abs (roundToInt (normRange.interval * 10000000));
  95. while ((v % 10) == 0)
  96. {
  97. --numDecimalPlaces;
  98. v /= 10;
  99. }
  100. }
  101. // keep the current values inside the new range..
  102. if (style != TwoValueHorizontal && style != TwoValueVertical)
  103. {
  104. setValue (getValue(), dontSendNotification);
  105. }
  106. else
  107. {
  108. setMinValue (getMinValue(), dontSendNotification, false);
  109. setMaxValue (getMaxValue(), dontSendNotification, false);
  110. }
  111. updateText();
  112. }
  113. void setRange (double newMin, double newMax, double newInt)
  114. {
  115. normRange = NormalisableRange<double> (newMin, newMax, newInt);
  116. updateRange();
  117. }
  118. void setNormalisableRange (NormalisableRange<double> newRange)
  119. {
  120. normRange = newRange;
  121. updateRange();
  122. }
  123. double getValue() const
  124. {
  125. // for a two-value style slider, you should use the getMinValue() and getMaxValue()
  126. // methods to get the two values.
  127. jassert (style != TwoValueHorizontal && style != TwoValueVertical);
  128. return currentValue.getValue();
  129. }
  130. void setValue (double newValue, NotificationType notification)
  131. {
  132. // for a two-value style slider, you should use the setMinValue() and setMaxValue()
  133. // methods to set the two values.
  134. jassert (style != TwoValueHorizontal && style != TwoValueVertical);
  135. newValue = constrainedValue (newValue);
  136. if (style == ThreeValueHorizontal || style == ThreeValueVertical)
  137. {
  138. jassert (static_cast<double> (valueMin.getValue()) <= static_cast<double> (valueMax.getValue()));
  139. newValue = jlimit (static_cast<double> (valueMin.getValue()),
  140. static_cast<double> (valueMax.getValue()),
  141. newValue);
  142. }
  143. if (newValue != lastCurrentValue)
  144. {
  145. if (valueBox != nullptr)
  146. valueBox->hideEditor (true);
  147. lastCurrentValue = newValue;
  148. // (need to do this comparison because the Value will use equalsWithSameType to compare
  149. // the new and old values, so will generate unwanted change events if the type changes)
  150. if (currentValue != newValue)
  151. currentValue = newValue;
  152. updateText();
  153. owner.repaint();
  154. updatePopupDisplay (newValue);
  155. triggerChangeMessage (notification);
  156. }
  157. }
  158. void setMinValue (double newValue, NotificationType notification, bool allowNudgingOfOtherValues)
  159. {
  160. // The minimum value only applies to sliders that are in two- or three-value mode.
  161. jassert (style == TwoValueHorizontal || style == TwoValueVertical
  162. || style == ThreeValueHorizontal || style == ThreeValueVertical);
  163. newValue = constrainedValue (newValue);
  164. if (style == TwoValueHorizontal || style == TwoValueVertical)
  165. {
  166. if (allowNudgingOfOtherValues && newValue > static_cast<double> (valueMax.getValue()))
  167. setMaxValue (newValue, notification, false);
  168. newValue = jmin (static_cast<double> (valueMax.getValue()), newValue);
  169. }
  170. else
  171. {
  172. if (allowNudgingOfOtherValues && newValue > lastCurrentValue)
  173. setValue (newValue, notification);
  174. newValue = jmin (lastCurrentValue, newValue);
  175. }
  176. if (lastValueMin != newValue)
  177. {
  178. lastValueMin = newValue;
  179. valueMin = newValue;
  180. owner.repaint();
  181. updatePopupDisplay (newValue);
  182. triggerChangeMessage (notification);
  183. }
  184. }
  185. void setMaxValue (double newValue, NotificationType notification, bool allowNudgingOfOtherValues)
  186. {
  187. // The maximum value only applies to sliders that are in two- or three-value mode.
  188. jassert (style == TwoValueHorizontal || style == TwoValueVertical
  189. || style == ThreeValueHorizontal || style == ThreeValueVertical);
  190. newValue = constrainedValue (newValue);
  191. if (style == TwoValueHorizontal || style == TwoValueVertical)
  192. {
  193. if (allowNudgingOfOtherValues && newValue < static_cast<double> (valueMin.getValue()))
  194. setMinValue (newValue, notification, false);
  195. newValue = jmax (static_cast<double> (valueMin.getValue()), newValue);
  196. }
  197. else
  198. {
  199. if (allowNudgingOfOtherValues && newValue < lastCurrentValue)
  200. setValue (newValue, notification);
  201. newValue = jmax (lastCurrentValue, newValue);
  202. }
  203. if (lastValueMax != newValue)
  204. {
  205. lastValueMax = newValue;
  206. valueMax = newValue;
  207. owner.repaint();
  208. updatePopupDisplay (valueMax.getValue());
  209. triggerChangeMessage (notification);
  210. }
  211. }
  212. void setMinAndMaxValues (double newMinValue, double newMaxValue, NotificationType notification)
  213. {
  214. // The maximum value only applies to sliders that are in two- or three-value mode.
  215. jassert (style == TwoValueHorizontal || style == TwoValueVertical
  216. || style == ThreeValueHorizontal || style == ThreeValueVertical);
  217. if (newMaxValue < newMinValue)
  218. std::swap (newMaxValue, newMinValue);
  219. newMinValue = constrainedValue (newMinValue);
  220. newMaxValue = constrainedValue (newMaxValue);
  221. if (lastValueMax != newMaxValue || lastValueMin != newMinValue)
  222. {
  223. lastValueMax = newMaxValue;
  224. lastValueMin = newMinValue;
  225. valueMin = newMinValue;
  226. valueMax = newMaxValue;
  227. owner.repaint();
  228. triggerChangeMessage (notification);
  229. }
  230. }
  231. double getMinValue() const
  232. {
  233. // The minimum value only applies to sliders that are in two- or three-value mode.
  234. jassert (style == TwoValueHorizontal || style == TwoValueVertical
  235. || style == ThreeValueHorizontal || style == ThreeValueVertical);
  236. return valueMin.getValue();
  237. }
  238. double getMaxValue() const
  239. {
  240. // The maximum value only applies to sliders that are in two- or three-value mode.
  241. jassert (style == TwoValueHorizontal || style == TwoValueVertical
  242. || style == ThreeValueHorizontal || style == ThreeValueVertical);
  243. return valueMax.getValue();
  244. }
  245. void triggerChangeMessage (NotificationType notification)
  246. {
  247. if (notification != dontSendNotification)
  248. {
  249. owner.valueChanged();
  250. if (notification == sendNotificationSync)
  251. handleAsyncUpdate();
  252. else
  253. triggerAsyncUpdate();
  254. }
  255. }
  256. void handleAsyncUpdate() override
  257. {
  258. cancelPendingUpdate();
  259. Component::BailOutChecker checker (&owner);
  260. listeners.callChecked (checker, [&] (Slider::Listener& l) { l.sliderValueChanged (&owner); });
  261. if (checker.shouldBailOut())
  262. return;
  263. if (owner.onValueChange != nullptr)
  264. owner.onValueChange();
  265. }
  266. void sendDragStart()
  267. {
  268. owner.startedDragging();
  269. Component::BailOutChecker checker (&owner);
  270. listeners.callChecked (checker, [&] (Slider::Listener& l) { l.sliderDragStarted (&owner); });
  271. if (checker.shouldBailOut())
  272. return;
  273. if (owner.onDragStart != nullptr)
  274. owner.onDragStart();
  275. }
  276. void sendDragEnd()
  277. {
  278. owner.stoppedDragging();
  279. sliderBeingDragged = -1;
  280. Component::BailOutChecker checker (&owner);
  281. listeners.callChecked (checker, [&] (Slider::Listener& l) { l.sliderDragEnded (&owner); });
  282. if (checker.shouldBailOut())
  283. return;
  284. if (owner.onDragEnd != nullptr)
  285. owner.onDragEnd();
  286. }
  287. struct DragInProgress
  288. {
  289. DragInProgress (Pimpl& p) : owner (p) { owner.sendDragStart(); }
  290. ~DragInProgress() { owner.sendDragEnd(); }
  291. Pimpl& owner;
  292. JUCE_DECLARE_NON_COPYABLE (DragInProgress)
  293. };
  294. void incrementOrDecrement (double delta)
  295. {
  296. if (style == IncDecButtons)
  297. {
  298. auto newValue = owner.snapValue (getValue() + delta, notDragging);
  299. if (currentDrag != nullptr)
  300. {
  301. setValue (newValue, sendNotificationSync);
  302. }
  303. else
  304. {
  305. DragInProgress drag (*this);
  306. setValue (newValue, sendNotificationSync);
  307. }
  308. }
  309. }
  310. void valueChanged (Value& value) override
  311. {
  312. if (value.refersToSameSourceAs (currentValue))
  313. {
  314. if (style != TwoValueHorizontal && style != TwoValueVertical)
  315. setValue (currentValue.getValue(), dontSendNotification);
  316. }
  317. else if (value.refersToSameSourceAs (valueMin))
  318. setMinValue (valueMin.getValue(), dontSendNotification, true);
  319. else if (value.refersToSameSourceAs (valueMax))
  320. setMaxValue (valueMax.getValue(), dontSendNotification, true);
  321. }
  322. void textChanged()
  323. {
  324. auto newValue = owner.snapValue (owner.getValueFromText (valueBox->getText()), notDragging);
  325. if (newValue != static_cast<double> (currentValue.getValue()))
  326. {
  327. DragInProgress drag (*this);
  328. setValue (newValue, sendNotificationSync);
  329. }
  330. updateText(); // force a clean-up of the text, needed in case setValue() hasn't done this.
  331. }
  332. void updateText()
  333. {
  334. if (valueBox != nullptr)
  335. {
  336. auto newValue = owner.getTextFromValue (currentValue.getValue());
  337. if (newValue != valueBox->getText())
  338. valueBox->setText (newValue, dontSendNotification);
  339. }
  340. }
  341. double constrainedValue (double value) const
  342. {
  343. return normRange.snapToLegalValue (value);
  344. }
  345. float getLinearSliderPos (double value) const
  346. {
  347. double pos;
  348. if (normRange.end <= normRange.start)
  349. pos = 0.5;
  350. else if (value < normRange.start)
  351. pos = 0.0;
  352. else if (value > normRange.end)
  353. pos = 1.0;
  354. else
  355. pos = owner.valueToProportionOfLength (value);
  356. if (isVertical() || style == IncDecButtons)
  357. pos = 1.0 - pos;
  358. jassert (pos >= 0 && pos <= 1.0);
  359. return (float) (sliderRegionStart + pos * sliderRegionSize);
  360. }
  361. void setSliderStyle (SliderStyle newStyle)
  362. {
  363. if (style != newStyle)
  364. {
  365. style = newStyle;
  366. owner.repaint();
  367. owner.lookAndFeelChanged();
  368. }
  369. }
  370. void setVelocityModeParameters (double sensitivity, int threshold,
  371. double offset, bool userCanPressKeyToSwapMode,
  372. ModifierKeys::Flags newModifierToSwapModes)
  373. {
  374. velocityModeSensitivity = sensitivity;
  375. velocityModeOffset = offset;
  376. velocityModeThreshold = threshold;
  377. userKeyOverridesVelocity = userCanPressKeyToSwapMode;
  378. modifierToSwapModes = newModifierToSwapModes;
  379. }
  380. void setIncDecButtonsMode (IncDecButtonMode mode)
  381. {
  382. if (incDecButtonMode != mode)
  383. {
  384. incDecButtonMode = mode;
  385. owner.lookAndFeelChanged();
  386. }
  387. }
  388. void setTextBoxStyle (TextEntryBoxPosition newPosition,
  389. bool isReadOnly,
  390. int textEntryBoxWidth,
  391. int textEntryBoxHeight)
  392. {
  393. if (textBoxPos != newPosition
  394. || editableText != (! isReadOnly)
  395. || textBoxWidth != textEntryBoxWidth
  396. || textBoxHeight != textEntryBoxHeight)
  397. {
  398. textBoxPos = newPosition;
  399. editableText = ! isReadOnly;
  400. textBoxWidth = textEntryBoxWidth;
  401. textBoxHeight = textEntryBoxHeight;
  402. owner.repaint();
  403. owner.lookAndFeelChanged();
  404. }
  405. }
  406. void setTextBoxIsEditable (bool shouldBeEditable)
  407. {
  408. editableText = shouldBeEditable;
  409. updateTextBoxEnablement();
  410. }
  411. void showTextBox()
  412. {
  413. jassert (editableText); // this should probably be avoided in read-only sliders.
  414. if (valueBox != nullptr)
  415. valueBox->showEditor();
  416. }
  417. void hideTextBox (bool discardCurrentEditorContents)
  418. {
  419. if (valueBox != nullptr)
  420. {
  421. valueBox->hideEditor (discardCurrentEditorContents);
  422. if (discardCurrentEditorContents)
  423. updateText();
  424. }
  425. }
  426. void setTextValueSuffix (const String& suffix)
  427. {
  428. if (textSuffix != suffix)
  429. {
  430. textSuffix = suffix;
  431. updateText();
  432. }
  433. }
  434. void updateTextBoxEnablement()
  435. {
  436. if (valueBox != nullptr)
  437. {
  438. bool shouldBeEditable = editableText && owner.isEnabled();
  439. if (valueBox->isEditable() != shouldBeEditable) // (to avoid changing the single/double click flags unless we need to)
  440. valueBox->setEditable (shouldBeEditable);
  441. }
  442. }
  443. void lookAndFeelChanged (LookAndFeel& lf)
  444. {
  445. if (textBoxPos != NoTextBox)
  446. {
  447. auto previousTextBoxContent = (valueBox != nullptr ? valueBox->getText()
  448. : owner.getTextFromValue (currentValue.getValue()));
  449. valueBox.reset();
  450. valueBox.reset (lf.createSliderTextBox (owner));
  451. owner.addAndMakeVisible (valueBox.get());
  452. valueBox->setWantsKeyboardFocus (false);
  453. valueBox->setText (previousTextBoxContent, dontSendNotification);
  454. valueBox->setTooltip (owner.getTooltip());
  455. updateTextBoxEnablement();
  456. valueBox->onTextChange = [this] { textChanged(); };
  457. if (style == LinearBar || style == LinearBarVertical)
  458. {
  459. valueBox->addMouseListener (&owner, false);
  460. valueBox->setMouseCursor (MouseCursor::ParentCursor);
  461. }
  462. }
  463. else
  464. {
  465. valueBox.reset();
  466. }
  467. if (style == IncDecButtons)
  468. {
  469. incButton.reset (lf.createSliderButton (owner, true));
  470. decButton.reset (lf.createSliderButton (owner, false));
  471. owner.addAndMakeVisible (incButton.get());
  472. owner.addAndMakeVisible (decButton.get());
  473. incButton->onClick = [this] { incrementOrDecrement (normRange.interval); };
  474. decButton->onClick = [this] { incrementOrDecrement (-normRange.interval); };
  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.reset();
  492. decButton.reset();
  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 += MathConstants<double>::twoPi;
  560. if (rotaryParams.stopAtEnd && e.mouseWasDraggedSinceMouseDown())
  561. {
  562. if (std::abs (angle - lastAngle) > MathConstants<double>::pi)
  563. {
  564. if (angle >= lastAngle)
  565. angle -= MathConstants<double>::twoPi;
  566. else
  567. angle += MathConstants<double>::twoPi;
  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 += MathConstants<double>::twoPi;
  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 (MathConstants<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.reset();
  664. popupDisplay.reset();
  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 (normRange.end > normRange.start)
  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.reset (new DragInProgress (*this));
  697. mouseDrag (e);
  698. }
  699. }
  700. }
  701. void mouseDrag (const MouseEvent& e)
  702. {
  703. if (useDragEvents && normRange.end > normRange.start
  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) || (normRange.end - normRange.start) / sliderRegionSize < normRange.interval)
  722. {
  723. dragMode = absoluteDrag;
  724. handleAbsoluteDrag (e);
  725. }
  726. else
  727. {
  728. dragMode = velocityDrag;
  729. handleVelocityDrag (e);
  730. }
  731. }
  732. valueWhenLastDragged = jlimit (normRange.start, normRange.end, 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. && (normRange.end > normRange.start)
  764. && (style != IncDecButtons || incDecDragged))
  765. {
  766. restoreMouseIfHidden();
  767. if (sendChangeOnlyOnRelease && valueOnMouseDown != static_cast<double> (currentValue.getValue()))
  768. triggerChangeMessage (sendNotificationAsync);
  769. currentDrag.reset();
  770. popupDisplay.reset();
  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.reset();
  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.reset();
  807. }
  808. void showPopupDisplay()
  809. {
  810. if (style == IncDecButtons)
  811. return;
  812. if (popupDisplay == nullptr)
  813. {
  814. popupDisplay.reset (new PopupDisplayComponent (owner));
  815. if (parentForPopupDisplay != nullptr)
  816. parentForPopupDisplay->addChildComponent (popupDisplay.get());
  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. && normRange.start <= doubleClickReturnValue
  842. && normRange.end >= 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 normRange.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 (normRange.end > normRange.start && ! 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 (normRange.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 && mods.testFlags (modifierToSwapModes));
  899. }
  900. void restoreMouseIfHidden()
  901. {
  902. for (auto& ms : Desktop::getInstance().getMouseSources())
  903. {
  904. if (ms.isUnboundedMouseMovementEnabled())
  905. {
  906. ms.enableUnboundedMouseMovement (false);
  907. auto pos = sliderBeingDragged == 2 ? getMaxValue()
  908. : (sliderBeingDragged == 1 ? getMinValue()
  909. : static_cast<double> (currentValue.getValue()));
  910. Point<float> mousePos;
  911. if (isRotary())
  912. {
  913. mousePos = ms.getLastMouseDownPosition();
  914. auto delta = (float) (pixelsForFullDragExtent * (owner.valueToProportionOfLength (valueOnMouseDown)
  915. - owner.valueToProportionOfLength (pos)));
  916. if (style == RotaryHorizontalDrag) mousePos += Point<float> (-delta, 0.0f);
  917. else if (style == RotaryVerticalDrag) mousePos += Point<float> (0.0f, delta);
  918. else mousePos += Point<float> (delta / -2.0f, delta / 2.0f);
  919. mousePos = owner.getScreenBounds().reduced (4).toFloat().getConstrainedPoint (mousePos);
  920. mouseDragStartPos = mousePosWhenLastDragged = owner.getLocalPoint (nullptr, mousePos);
  921. valueOnMouseDown = valueWhenLastDragged;
  922. }
  923. else
  924. {
  925. auto pixelPos = (float) getLinearSliderPos (pos);
  926. mousePos = owner.localPointToGlobal (Point<float> (isHorizontal() ? pixelPos : (owner.getWidth() / 2.0f),
  927. isVertical() ? pixelPos : (owner.getHeight() / 2.0f)));
  928. }
  929. const_cast <MouseInputSource&> (ms).setScreenPosition (mousePos);
  930. }
  931. }
  932. }
  933. //==============================================================================
  934. void paint (Graphics& g, LookAndFeel& lf)
  935. {
  936. if (style != IncDecButtons)
  937. {
  938. if (isRotary())
  939. {
  940. auto sliderPos = (float) owner.valueToProportionOfLength (lastCurrentValue);
  941. jassert (sliderPos >= 0 && sliderPos <= 1.0f);
  942. lf.drawRotarySlider (g,
  943. sliderRect.getX(), sliderRect.getY(),
  944. sliderRect.getWidth(), sliderRect.getHeight(),
  945. sliderPos, rotaryParams.startAngleRadians,
  946. rotaryParams.endAngleRadians, owner);
  947. }
  948. else
  949. {
  950. lf.drawLinearSlider (g,
  951. sliderRect.getX(), sliderRect.getY(),
  952. sliderRect.getWidth(), sliderRect.getHeight(),
  953. getLinearSliderPos (lastCurrentValue),
  954. getLinearSliderPos (lastValueMin),
  955. getLinearSliderPos (lastValueMax),
  956. style, owner);
  957. }
  958. if ((style == LinearBar || style == LinearBarVertical) && valueBox == nullptr)
  959. {
  960. g.setColour (owner.findColour (Slider::textBoxOutlineColourId));
  961. g.drawRect (0, 0, owner.getWidth(), owner.getHeight(), 1);
  962. }
  963. }
  964. }
  965. //==============================================================================
  966. void resized (LookAndFeel& lf)
  967. {
  968. auto layout = lf.getSliderLayout (owner);
  969. sliderRect = layout.sliderBounds;
  970. if (valueBox != nullptr)
  971. valueBox->setBounds (layout.textBoxBounds);
  972. if (isHorizontal())
  973. {
  974. sliderRegionStart = layout.sliderBounds.getX();
  975. sliderRegionSize = layout.sliderBounds.getWidth();
  976. }
  977. else if (isVertical())
  978. {
  979. sliderRegionStart = layout.sliderBounds.getY();
  980. sliderRegionSize = layout.sliderBounds.getHeight();
  981. }
  982. else if (style == IncDecButtons)
  983. {
  984. resizeIncDecButtons();
  985. }
  986. }
  987. //==============================================================================
  988. void resizeIncDecButtons()
  989. {
  990. auto buttonRect = sliderRect;
  991. if (textBoxPos == TextBoxLeft || textBoxPos == TextBoxRight)
  992. buttonRect.expand (-2, 0);
  993. else
  994. buttonRect.expand (0, -2);
  995. incDecButtonsSideBySide = buttonRect.getWidth() > buttonRect.getHeight();
  996. if (incDecButtonsSideBySide)
  997. {
  998. decButton->setBounds (buttonRect.removeFromLeft (buttonRect.getWidth() / 2));
  999. decButton->setConnectedEdges (Button::ConnectedOnRight);
  1000. incButton->setConnectedEdges (Button::ConnectedOnLeft);
  1001. }
  1002. else
  1003. {
  1004. decButton->setBounds (buttonRect.removeFromBottom (buttonRect.getHeight() / 2));
  1005. decButton->setConnectedEdges (Button::ConnectedOnTop);
  1006. incButton->setConnectedEdges (Button::ConnectedOnBottom);
  1007. }
  1008. incButton->setBounds (buttonRect);
  1009. }
  1010. //==============================================================================
  1011. Slider& owner;
  1012. SliderStyle style;
  1013. ListenerList<Slider::Listener> listeners;
  1014. Value currentValue, valueMin, valueMax;
  1015. double lastCurrentValue = 0, lastValueMin = 0, lastValueMax = 0;
  1016. NormalisableRange<double> normRange { 0.0, 10.0 };
  1017. double doubleClickReturnValue = 0;
  1018. double valueWhenLastDragged = 0, valueOnMouseDown = 0, lastAngle = 0;
  1019. double velocityModeSensitivity = 1.0, velocityModeOffset = 0, minMaxDiff = 0;
  1020. int velocityModeThreshold = 1;
  1021. RotaryParameters rotaryParams;
  1022. Point<float> mouseDragStartPos, mousePosWhenLastDragged;
  1023. int sliderRegionStart = 0, sliderRegionSize = 1;
  1024. int sliderBeingDragged = -1;
  1025. int pixelsForFullDragExtent = 250;
  1026. Time lastMouseWheelTime;
  1027. Rectangle<int> sliderRect;
  1028. ScopedPointer<DragInProgress> currentDrag;
  1029. TextEntryBoxPosition textBoxPos;
  1030. String textSuffix;
  1031. int numDecimalPlaces = 7;
  1032. int textBoxWidth = 80, textBoxHeight = 20;
  1033. IncDecButtonMode incDecButtonMode = incDecButtonsNotDraggable;
  1034. ModifierKeys::Flags modifierToSwapModes = ModifierKeys::ctrlAltCommandModifiers;
  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.reset();
  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 + MathConstants<double>::twoPi - a2),
  1103. std::abs (a2 + MathConstants<double>::twoPi - 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.reset (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 < MathConstants<float>::pi * 4.0f
  1140. && p.endAngleRadians < MathConstants<float>::pi * 4.0f);
  1141. pimpl->rotaryParams = p;
  1142. }
  1143. void Slider::setRotaryParameters (float startAngleRadians, float endAngleRadians, bool stopAtEnd) noexcept
  1144. {
  1145. setRotaryParameters ({ startAngleRadians, endAngleRadians, stopAtEnd });
  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,
  1158. double offset, bool userCanPressKeyToSwapMode,
  1159. ModifierKeys::Flags modifierToSwapModes)
  1160. {
  1161. jassert (threshold >= 0);
  1162. jassert (sensitivity > 0);
  1163. jassert (offset >= 0);
  1164. pimpl->setVelocityModeParameters (sensitivity, threshold, offset,
  1165. userCanPressKeyToSwapMode, modifierToSwapModes);
  1166. }
  1167. double Slider::getSkewFactor() const noexcept { return pimpl->normRange.skew; }
  1168. bool Slider::isSymmetricSkew() const noexcept { return pimpl->normRange.symmetricSkew; }
  1169. void Slider::setSkewFactor (double factor, bool symmetricSkew)
  1170. {
  1171. pimpl->normRange.skew = factor;
  1172. pimpl->normRange.symmetricSkew = symmetricSkew;
  1173. }
  1174. void Slider::setSkewFactorFromMidPoint (double sliderValueToShowAtMidPoint)
  1175. {
  1176. pimpl->normRange.setSkewForCentre (sliderValueToShowAtMidPoint);
  1177. }
  1178. int Slider::getMouseDragSensitivity() const noexcept { return pimpl->pixelsForFullDragExtent; }
  1179. void Slider::setMouseDragSensitivity (int distanceForFullScaleDrag)
  1180. {
  1181. jassert (distanceForFullScaleDrag > 0);
  1182. pimpl->pixelsForFullDragExtent = distanceForFullScaleDrag;
  1183. }
  1184. void Slider::setIncDecButtonsMode (IncDecButtonMode mode) { pimpl->setIncDecButtonsMode (mode); }
  1185. Slider::TextEntryBoxPosition Slider::getTextBoxPosition() const noexcept { return pimpl->textBoxPos; }
  1186. int Slider::getTextBoxWidth() const noexcept { return pimpl->textBoxWidth; }
  1187. int Slider::getTextBoxHeight() const noexcept { return pimpl->textBoxHeight; }
  1188. void Slider::setTextBoxStyle (TextEntryBoxPosition newPosition, bool isReadOnly, int textEntryBoxWidth, int textEntryBoxHeight)
  1189. {
  1190. pimpl->setTextBoxStyle (newPosition, isReadOnly, textEntryBoxWidth, textEntryBoxHeight);
  1191. }
  1192. bool Slider::isTextBoxEditable() const noexcept { return pimpl->editableText; }
  1193. void Slider::setTextBoxIsEditable (const bool shouldBeEditable) { pimpl->setTextBoxIsEditable (shouldBeEditable); }
  1194. void Slider::showTextBox() { pimpl->showTextBox(); }
  1195. void Slider::hideTextBox (bool discardCurrentEditorContents) { pimpl->hideTextBox (discardCurrentEditorContents); }
  1196. void Slider::setChangeNotificationOnlyOnRelease (bool onlyNotifyOnRelease)
  1197. {
  1198. pimpl->sendChangeOnlyOnRelease = onlyNotifyOnRelease;
  1199. }
  1200. bool Slider::getSliderSnapsToMousePosition() const noexcept { return pimpl->snapsToMousePos; }
  1201. void Slider::setSliderSnapsToMousePosition (bool shouldSnapToMouse) { pimpl->snapsToMousePos = shouldSnapToMouse; }
  1202. void Slider::setPopupDisplayEnabled (bool showOnDrag, bool showOnHover, Component* parent, int hoverTimeout)
  1203. {
  1204. pimpl->showPopupOnDrag = showOnDrag;
  1205. pimpl->showPopupOnHover = showOnHover;
  1206. pimpl->parentForPopupDisplay = parent;
  1207. pimpl->popupHoverTimeout = hoverTimeout;
  1208. }
  1209. Component* Slider::getCurrentPopupDisplay() const noexcept { return pimpl->popupDisplay.get(); }
  1210. //==============================================================================
  1211. void Slider::colourChanged() { lookAndFeelChanged(); }
  1212. void Slider::lookAndFeelChanged() { pimpl->lookAndFeelChanged (getLookAndFeel()); }
  1213. void Slider::enablementChanged() { repaint(); pimpl->updateTextBoxEnablement(); }
  1214. //==============================================================================
  1215. Range<double> Slider::getRange() const noexcept { return { pimpl->normRange.start, pimpl->normRange.end }; }
  1216. double Slider::getMaximum() const noexcept { return pimpl->normRange.end; }
  1217. double Slider::getMinimum() const noexcept { return pimpl->normRange.start; }
  1218. double Slider::getInterval() const noexcept { return pimpl->normRange.interval; }
  1219. void Slider::setRange (double newMin, double newMax, double newInt) { pimpl->setRange (newMin, newMax, newInt); }
  1220. void Slider::setRange (Range<double> newRange, double newInt) { pimpl->setRange (newRange.getStart(), newRange.getEnd(), newInt); }
  1221. void Slider::setNormalisableRange (NormalisableRange<double> newRange) { pimpl->setNormalisableRange (newRange); }
  1222. double Slider::getValue() const { return pimpl->getValue(); }
  1223. Value& Slider::getValueObject() noexcept { return pimpl->currentValue; }
  1224. Value& Slider::getMinValueObject() noexcept { return pimpl->valueMin; }
  1225. Value& Slider::getMaxValueObject() noexcept { return pimpl->valueMax; }
  1226. void Slider::setValue (double newValue, NotificationType notification)
  1227. {
  1228. pimpl->setValue (newValue, notification);
  1229. }
  1230. double Slider::getMinValue() const { return pimpl->getMinValue(); }
  1231. double Slider::getMaxValue() const { return pimpl->getMaxValue(); }
  1232. void Slider::setMinValue (double newValue, NotificationType notification, bool allowNudgingOfOtherValues)
  1233. {
  1234. pimpl->setMinValue (newValue, notification, allowNudgingOfOtherValues);
  1235. }
  1236. void Slider::setMaxValue (double newValue, NotificationType notification, bool allowNudgingOfOtherValues)
  1237. {
  1238. pimpl->setMaxValue (newValue, notification, allowNudgingOfOtherValues);
  1239. }
  1240. void Slider::setMinAndMaxValues (double newMinValue, double newMaxValue, NotificationType notification)
  1241. {
  1242. pimpl->setMinAndMaxValues (newMinValue, newMaxValue, notification);
  1243. }
  1244. void Slider::setDoubleClickReturnValue (bool isDoubleClickEnabled, double valueToSetOnDoubleClick)
  1245. {
  1246. pimpl->doubleClickToValue = isDoubleClickEnabled;
  1247. pimpl->doubleClickReturnValue = valueToSetOnDoubleClick;
  1248. }
  1249. double Slider::getDoubleClickReturnValue() const noexcept { return pimpl->doubleClickReturnValue; }
  1250. bool Slider::isDoubleClickReturnEnabled() const noexcept { return pimpl->doubleClickToValue; }
  1251. void Slider::updateText()
  1252. {
  1253. pimpl->updateText();
  1254. }
  1255. void Slider::setTextValueSuffix (const String& suffix)
  1256. {
  1257. pimpl->setTextValueSuffix (suffix);
  1258. }
  1259. String Slider::getTextValueSuffix() const
  1260. {
  1261. return pimpl->textSuffix;
  1262. }
  1263. String Slider::getTextFromValue (double v)
  1264. {
  1265. if (textFromValueFunction != nullptr)
  1266. return textFromValueFunction (v);
  1267. if (getNumDecimalPlacesToDisplay() > 0)
  1268. return String (v, getNumDecimalPlacesToDisplay()) + getTextValueSuffix();
  1269. return String (roundToInt (v)) + getTextValueSuffix();
  1270. }
  1271. double Slider::getValueFromText (const String& text)
  1272. {
  1273. if (valueFromTextFunction != nullptr)
  1274. return valueFromTextFunction (text);
  1275. auto t = text.trimStart();
  1276. if (t.endsWith (getTextValueSuffix()))
  1277. t = t.substring (0, t.length() - getTextValueSuffix().length());
  1278. while (t.startsWithChar ('+'))
  1279. t = t.substring (1).trimStart();
  1280. return t.initialSectionContainingOnly ("0123456789.,-")
  1281. .getDoubleValue();
  1282. }
  1283. double Slider::proportionOfLengthToValue (double proportion)
  1284. {
  1285. return pimpl->normRange.convertFrom0to1 (proportion);
  1286. }
  1287. double Slider::valueToProportionOfLength (double value)
  1288. {
  1289. return pimpl->normRange.convertTo0to1 (value);
  1290. }
  1291. double Slider::snapValue (double attemptedValue, DragMode)
  1292. {
  1293. return attemptedValue;
  1294. }
  1295. int Slider::getNumDecimalPlacesToDisplay() const noexcept { return pimpl->numDecimalPlaces; }
  1296. void Slider::setNumDecimalPlacesToDisplay (int decimalPlacesToDisplay)
  1297. {
  1298. pimpl->numDecimalPlaces = decimalPlacesToDisplay;
  1299. updateText();
  1300. }
  1301. //==============================================================================
  1302. int Slider::getThumbBeingDragged() const noexcept { return pimpl->sliderBeingDragged; }
  1303. void Slider::startedDragging() {}
  1304. void Slider::stoppedDragging() {}
  1305. void Slider::valueChanged() {}
  1306. //==============================================================================
  1307. void Slider::setPopupMenuEnabled (bool menuEnabled) { pimpl->menuEnabled = menuEnabled; }
  1308. void Slider::setScrollWheelEnabled (bool enabled) { pimpl->scrollWheelEnabled = enabled; }
  1309. bool Slider::isHorizontal() const noexcept { return pimpl->isHorizontal(); }
  1310. bool Slider::isVertical() const noexcept { return pimpl->isVertical(); }
  1311. bool Slider::isRotary() const noexcept { return pimpl->isRotary(); }
  1312. bool Slider::isBar() const noexcept { return pimpl->isBar(); }
  1313. float Slider::getPositionOfValue (double value) const { return pimpl->getPositionOfValue (value); }
  1314. //==============================================================================
  1315. void Slider::paint (Graphics& g) { pimpl->paint (g, getLookAndFeel()); }
  1316. void Slider::resized() { pimpl->resized (getLookAndFeel()); }
  1317. void Slider::focusOfChildComponentChanged (FocusChangeType) { repaint(); }
  1318. void Slider::mouseDown (const MouseEvent& e) { pimpl->mouseDown (e); }
  1319. void Slider::mouseUp (const MouseEvent&) { pimpl->mouseUp(); }
  1320. void Slider::mouseMove (const MouseEvent&) { pimpl->mouseMove(); }
  1321. void Slider::mouseExit (const MouseEvent&) { pimpl->mouseExit(); }
  1322. // If popup display is enabled and set to show on mouse hover, this makes sure
  1323. // it is shown when dragging the mouse over a slider and releasing
  1324. void Slider::mouseEnter (const MouseEvent&) { pimpl->mouseMove(); }
  1325. void Slider::modifierKeysChanged (const ModifierKeys& modifiers)
  1326. {
  1327. if (isEnabled())
  1328. pimpl->modifierKeysChanged (modifiers);
  1329. }
  1330. void Slider::mouseDrag (const MouseEvent& e)
  1331. {
  1332. if (isEnabled())
  1333. pimpl->mouseDrag (e);
  1334. }
  1335. void Slider::mouseDoubleClick (const MouseEvent&)
  1336. {
  1337. if (isEnabled())
  1338. pimpl->mouseDoubleClick();
  1339. }
  1340. void Slider::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel)
  1341. {
  1342. if (! (isEnabled() && pimpl->mouseWheelMove (e, wheel)))
  1343. Component::mouseWheelMove (e, wheel);
  1344. }
  1345. } // namespace juce