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.

1575 lines
57KB

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